mai () function php not working under conditions

3

I have a PHP sending a contact form email, when it is in the captcha check condition, I receive the sent message, but the email is not delivered, and when I shoot the condition the email arrives normal. Follow the PHP code (The submit is done by ajax)

    <?php

$captcha = $_POST['captcha'];
if (!empty($_POST['captcha'])) {

    $resposta = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRET-KEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);

    if ($resposta.success) {
        $to = "[email protected]";
        $nome = $_POST["nome"];
        $email = $_POST["email"];
        $mensagem = $_POST["mensagem"];
        $celular = $_POST["celular"];
        $assunto = $_POST["assunto"];
        $txt = "MENSAGEM: $mensagem". "\r\n" . "CELULAR: $celular". "\r\n" . "NOME: $nome";
        $headers = "From: $email";

        if ((empty($nome))||(empty($email))||(empty($celular))||(empty($mensagem))||(empty($assunto))) {
            print('Preencha os campos');
        } else {
            $envio = mail($to,$assunto,$txt,$headers);
            if ($envio) {
                print('Enviado');
            }else{
                print('Erro');
            }
        }
    } else {
        print('Erro');
    }
} else {
    print('Marque o Captcha');
}

?>
    
asked by anonymous 14.06.2016 / 19:05

2 answers

0

You should have to set some headers (not default) to 'trick' some email clients that might think it is spam, namely From and Reply-To , set your headers like this:

$subject = 'Ajudar Vinicius a mandar email ';
$body = 'corpo da mensagem';
$to = 'EMAIL@DESTINO'; // ajustar
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: Miguel <[email protected]>" . "\r\n" .
"Reply-To: [email protected]" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

if(!mail($to, $subject, $body, $headers)) 
{
    echo 'Erro';
}
else {
    echo 'YAY';
}
    
14.06.2016 / 21:11
2

Depending on your hosting server (A Locaweb for example), you need to authenticate to send email.

If authentication is required, use PHPMailer to make the submission, the configuration simpler than in php.ini.

    
14.06.2016 / 19:59