Form data does not send to email

6

I made a small form for sending e-mail. But when I test the send, nothing arrives in the destination email. As I do not have much experience in this part I could not identify the error, since when I send it does not give any. Here's 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");
    
asked by anonymous 11.02.2015 / 21:13

2 answers

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 contato.php file you should treat the $_GET["msg"] variable for each possible value, and display the appropriate message to the user.

    
12.02.2015 / 04:34
2

The function mail() returns false if it does not, use an 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 this is the basics, now you have to analyze how your server is, 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 465 with ssl

I've already had several problems with this function and in the end I ended up choosing a library called phpmailer. it makes sending and debugging a lot easier.

Topics about it in StackOverflow

Shipping example

    
10.11.2015 / 12:38