mai() php function not working under conditions


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)


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

    $resposta = file_get_contents("".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);

    if ($resposta.success) {
        $to = "";
        $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) {
    } else {
} else {
    print('Marque o Captcha');



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