PHP form is not sending the email

Asked

Viewed 330 times

0

I have the following HTML code:

<form action="send-mail.php" method="post">
    <div class="templatemo_form">
        <input name="name" type="text" class="form-control" id="name" placeholder="Seu Nome" maxlength="40" required/>
    </div>
    <div class="templatemo_form">
        <input name="email" type="email" class="form-control" id="email" placeholder="Seu Email" maxlength="40" required/>
    </div>
    <div class="templatemo_form">
        <input name="assunto" type="text" class="form-control" id="assunto" placeholder="Assunto" maxlength="60" required/>
    </div>
    <div class="templatemo_form">
        <textarea name="mensagem" class="form-control" id="mensagem" placeholder="Mensagem"></textarea>
    </div>
    <div class="templatemo_form">
        <input name="enviar" type="submit" class="btn btn-primary" value="Enviar" />
    </div>
</form>

And the upload PHP file is:

<?php
    $name     =   $_POST['name']; //pega os dados que foi digitado no ID name.
    $email    =   $_POST['email']; //pega os dados que foi digitado no ID email.
    $assunto  =   $_POST['assunto']; //pega os dados que foi digitado no ID assunto.
    $mensagem  =   $_POST['mensagem']; //pega os dados que foi digitado no ID mensagem.

    $headers  = "From: $email\r\n";
    $headers .= "Reply-To: $email\r\n";

/* abaixo contém os dados que serão enviados para o email cadastrado para receber o formulário */

    $corpo = "Formulario Enviado\n";
    $corpo .= "Nome: " . $name . "\n";
    $corpo .= "E-mail: " . $email . "\n";
    $corpo .= "Assunto: " . $assunto . "\n";
    $corpo .= "Mensagem: " . $mensagem . "\n";


    $email_to = '[email protected]'; //não esqueça de substituir este email pelo seu.

    $status = mail($email_to, $email, $corpo, $headers); //enviando o email.

    if($status) {       
       echo "<script> alert('Obrigado pela mensagem. Em breve entraremos em contato.'); </script>"; //mensagem de form enviado com sucesso.     
    }   else {      
       echo "<script alert('Falha ao enviar a mensagem.'); </script>"; //mensagem de erro no envio.     
    }   
    echo "<script> window.location.href = 'index.html'; </script>"; //mudar o  site para redirecionar após o envio do form.
?>

He does everything right but the email is not enough. Can anyone tell me what might be wrong?

  • Is working locally?

  • Try placing the following headers $headers = "MIME-Version: 1.0" . "\r\n";&#xA; $headers .= "Content-type: text/html; charset=utf-8" . "\r\n";&#xA; $headers .= "From: Luiz <[email protected]>" . "\r\n" .&#xA; "Reply-To: [email protected]" . "\r\n" .&#xA; "X-Mailer: PHP/" . phpversion();

  • And the connection, okay ?

  • Is your server SMTP configured? Have you tried Codeigniter? It will greatly simplify everything you are using there...

  • Lazyfox, it worked. Thanks

  • Sorry, it was Miguel who helped

Show 1 more comment

1 answer

0

Depending on the server you are on, it doesn’t just send from the server it needs a class to configure smtp. In this case when it happens I use PHPMAILER to work. Remembering that it is very important to know that the upload configuration $mail->Username and $mail->Password = 'password' must be an email from the same server from which the form is hosted.

See where you have these settings:

$mail->Issmtp(); // Defines the message to be SMTP $mail->Host = "smtp.dominio.net"; // SMTP server address $mail->Smtpauth = true; // Use SMTP authentication? (optional) $mail->Username = '[email protected]'; // SMTP server user $mail->Password = 'password'; // SMTP server password

An example of servers that work like this is localweb.

see if this link helps you it explains very good how to use the phpmailer

  • Thanks Paulinha, but my server is Uol and for what I saw it does not need to configure SMTP

  • (http://www.lhost.uol.com.br/faq/accomodation/como-sendmessengers-com-php-authenticationo-smtp.html) tries these instructions, either way need to authenticate the shipment or will not go

Browser other questions tagged

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