PHP form is not sending the email

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 sending 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 could be wrong?

Author: stderr, 2016-09-22

1 answers

Depending on the server you find, it does not send only by the server it needs a class that configures smtp. In this case qdo it happens I use PHPMAILER to work. Remembering that it is very important to know that the configuration of sending mail mail->Username and $mail->Password = 'Password' must be from an email from the same server from which the form is hosted.

See where you have these settings from:

Mail mail- > IsSMTP(); // Defines that the message will be SMTP mail mail->Host = "smtp.dominio.net"; / / SMTP server address Usa mail->SMTPAuth = true; // use SMTP authentication? (optional) mail mail->Username = '[email protected]'; / / SMTP server user mail 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 phpMailer

 0
Author: Paulinha, 2016-09-22 18:49:21