mai() php function not working under conditions

Asked

Viewed 77 times

3

I have a PHP that sends an email from a contact form, when this in the condition of the captcha verification, I receive the sent message, but the email is not delivered, and when I remove 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');
}

?>
  • Returns some of the prints?

  • Yes, I printed all normal ones by not checking captcha, if not all fields are filled and when sending.

  • What do you print? enters what condition?

  • If you check the captcha and fill in all the fields, I printed: Sent. The prints come out as if they were working, but the email doesn’t arrive.

  • If you are on localhost you will not send unless you have an email server installed

  • It is on a server. As I said: when I remove the mail() from this condition, it works, that is, I receive the email sent by php.

  • I think I know what’s happening... It must be because your email client is viewing it as spam. I’m going to post a possible solution

  • Depending on the hosting does not accept this function, uses Phpmail.

Show 3 more comments

2 answers

2

Depending on your hosting server (The Locaweb for example), authentication is required to perform email sending.

If authentication is required, use Phpmailer to upload, the configuration is simpler than in php.ini.

  • I think there is no problem in authentication because when I remove Mail from condition, sending works normally.

0


You must have to define some headers (not default ones) to 'trick' some email clients who might think it is spam, namely the From and the 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';
}

Browser other questions tagged

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