Form data does not send to email

Asked

Viewed 378 times

6

I made a short form to send e-mail. But when I do the sending test nothing arrives in the destination email. As I do not have much experience in this part I could not identify the error, since when sending does not give any. Follow my HTML:

<!doctype html>
<html lang="pt-br">
<head>
<? include "includes/head.php"?>
</head>
<body>
<div id="site">
<div class="container">
    <section class="contato">
        <? include "includes/topo.php"?>
        <div class="envolve">
            <h2>CONTATO</h2>
            <div>
                <p>Preencha o formulário</p>
                <form id="form1" name="form1" method="post" action="mail.php">
                    <input type="text" name="nome" placeholder="Nome:" required>
                    <input type="email" name="email" placeholder="E-mail:" required>
                    <input type="tel" name="telefone" placeholder="Telefone:" required>
                    <input type="text" name="estado" placeholder="Estado:" required>
                    <textarea name="mensagem" placeholder="Mensagem:"></textarea>
                    <input type="submit" name="enviar" value="Enviar">
                </form>
            </div>          
            <div>
                <p>Av. Sumare, 1642 - Sumare - SP</p>
                <p><strong>Contratação de artistas</strong></p> 
                <p>(11) 2977-5177</p>
                <span class="fr">
                <iframe src="https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d3657.666898030628!2d-46.676697399999995!3d-23.544480200000002!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x94ce578cc9a3c755%3A0xe7e810c950cee00b!2sAv.+Sumar%C3%A9%2C+1642+-+Sumar%C3%A9%2C+S%C3%A3o+Paulo+-+SP%2C+01252-120!5e0!3m2!1spt-BR!2sbr!4v1423246864426" width="412" height="298" frameborder="0" style="border:0"></iframe>
                </span>
            </div>
        </div>
    </section>
</div> 
<? include "includes/rodape.php"?>    
</div>
</body>
</html>

My PHP code:

$para     = "[email protected]";
$assunto  = "Contato pelo site";
$nome     = $_POST['nome'];
$email    = $_POST['email'];
$telefone = $_POST['telefone'];
$estado   = $_POST['estado'];
$mensagem = $_POST['mensagem'];

$corpo  = "<strong>Mensagem de contato<br><br></strong>";
$corpo .= "<strong>Nome:</strong>$nome";
$corpo .= "<br><strong>E-mail:</strong>$email";
$corpo .= "<br><strong>Telefone:</strong>$telefone";
$corpo .= "<br><strong>Estado:</strong>$estado";
$corpo .= "<br><strong>Mensagem:</strong>$mensagem";

$header .= "Content-Type: text/html; charset= utf-8\n";
$header = "From: $email Reply-to: $email\n";    

@mail($para,$assunto,$corpo,$header);

header("location:contato.php?msg=enviado");
  • Look in the recipient’s SPAM box as you are using mail function. Anything is possible.

  • 1

    I recommend my reply, to send email by SMTP: http://answall.com/questions/45556/funcao-mail-no-php/45568#45568

  • mail returns true or false. Then use this to your advantage. However, as stated in my reply http://answall.com/a/45558/3635 it is not possible to verify that the e-mail has reached the recipient, only that it has reached the e-mail server service. But as you said @Qmechanic73 may be some error in your code that you have hidden.

  • Lollipop this was the first thing I did I looked in all of the mailboxes, as for the link you went through from phpmailer, they serve for all the servers? For example the server I am testing is from Locaweb. Qmechanic had already tested without the @ error

  • if my code is correct, then the problem is the configuration of the server?

  • William as I had already told Qmechanic does not give any error when I take the @ simply writes sent in the url but does not send any email

  • 1

    Wait a minute, you said the form data doesn’t go to the email, so that means you get the email, but it comes without the completed data? Or do you not even receive the email? And please you did the test of TRUE and FALSE I said?

  • @Guilherme Nascimento, the data is sent in the url but nothing comes to the email, as for that test to take @, I’ve done it and there is no mistake

  • Appears as sent by which you added a redirect. You should use TRUE/FALSE with IF something like: if(mail()) { header(); } Else { echo 'error'; }, if even so the page redirects, then your problem is not with PHP but with Antispam or something like that.

  • check your hosting provider’s email submission standards.. most do not allow direct sending without authentication or through specific settings. Then refer to your hosting provider’s documentation and support.

  • @user4451 goes to phpmailer. With it you set up an email account for sending and it is not dependent on each server where your app will run.

Show 6 more comments

2 answers

2

The function mail() returns false if not send, use if to check if it was sent successfully.

Example:

if(mail($para,$assunto,$corpo,$header)){
    // enviado com sucesso. faça algo
}else{
    echo "Não foi possivel enviar o email.";
}

Well that’s the basics, now you have to analyze how this your server, and check on your php.ini the following parameters:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = [email protected]

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail().
;mail.force_extra_parameters =

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = On

; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
;mail.log =
; Log mail to syslog (Event Log on Windows).
;mail.log = syslog

First check which port you are trying to send, when I try to send to gmail the port I use is the 465 with ssl

I have already passed several problems with this function and in the end I chose a library called phpmailer. it makes it much easier to send and debug.

Topics about her on Stackoverflow

Sending example

2

You did:

// A variável $header não existia mas, se sua conf do PHP permitir, ela será criada
// e nenhum erro será lançado, caso contrário o servidor retornará um erro 500
$header .= "Content-Type: text/html; charset= utf-8\n";

// Aqui você está usando dois cabeçalhos diferentes na mesma linha, o que é inválido
// From: $email
// Reply-to: $email
// E a atribuição anterior é apagada
$header = "From: $email Reply-to: $email\n"; 

I recommend changing the end of the code to:

$headers = array(
    "Content-Type: text/html; charset= utf-8",
    "From: $email",
    "Reply-to: $email"
);

$retorno = @mail($para, $assunto, $corpo, implode("\r\n", $headers));

if($retorno) $msg = "enviado";
else $msg = "nao_enviado";

header("location:contato.php?msg=$msg");

?>

In your file contato.php you must treat the variable $_GET["msg"] for each possible value, and display the appropriate message to the user.

  • I did as you said and I changed the code for what you suggested and it continues the same way. As for treating variable, as I had said at the beginning of the post my knowledge is not so great in php and I don’t know how to handle variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.