mai() php function not working under conditions

Question:

I have a PHP that sends an email from a contact form, when it is in the captcha verification condition, I receive the sent message, but the email is not delivered, and when I take it out of the condition, the email arrives normally. Follows the PHP code (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@gmail.com";
        $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');
}

?>

Answer:

Depending on your hosting server (Locaweb for example), it is necessary to authenticate to send email.

If authentication is mandatory, use PHPMailer to perform the sending, the configuration is simpler than in php.ini.

Scroll to Top