Search code examples
javascriptajaxphpmailer

Ajax and PHPMailer error


I'm improving a code, but i'm getting some troubles.

I'm trying to send an email to one of my users. If the email exist i got no problem, instead if doesn't exist the website app explode.

This is my js code using ajax:

callAjax(url, paramsInsert, true, function (data) {
    if (data.success) {
        modal("OK", "<h3>ALL OK</h3>", "OK");
    } else {
        modal("error", "<h3> ERROR </h3>", "Error");
    }
});

And this is my Ajax function:

function callAjax(url, parametros, load, funcion) {

if (load) {
    var element = document.createElement("div");
    element.id = "loadBackground";
    element.className = "backLoad";
    document.body.appendChild(element);
    document.getElementById("contenido").innerHTML = '<div class="loadImage"><div class="col-xs-12 col-sm-12 col-md-12"><div class="col-xs-4 col-sm-4 col-md-4"></div><div class="col-xs-4 col-sm-4 col-md-4"><img class="center-block" src="img/ajax-loader.gif" /></div><div class="col-xs-4 col-sm-4 col-md-4"></div></div></div>';
}

var ajax = new XMLHttpRequest();

ajax.onreadystatechange = function () {
    if (ajax.readyState === 4) {
        //var datos = JSON.parse(ajax.responseText);

        var jsontext = ajax.responseText;

        var content = eval('(' + jsontext + ')');
        //if (content.success) {
        funcion(content);
        if (load) {
            document.getElementById("contenido").innerHTML = '';
            document.body.removeChild(document.getElementById("loadBackground"));
        }
        // }

    }
};
ajax.open("POST", url, true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.send(parametros);

}

If mail is incorrect i'm getting this error on console of the broswer:

jsontext = "SMTP Error: The following recipients failed: [email protected]: : Recipient address rejected: Domain not found ↵{"success":false}"

The problem is that i don't get inside the else "callAjax" function.

How can i get inside that else when i get an error on wrong mail?

EDIT:

My php code is something like this:

$enviarEmail = $this->sendMAIL($_SESSION['idenUser'], $totalconDescuento, $envioDesc, $_POST['sin_assistente']);

    if (!$enviarEmail) {
        echo json_encode(['success' => false, 'jsontext' => 'Error']);
    } else {
        echo json_encode(['success' => true, 'jsontext' => 'OK']);
    }

Still getting same error:

jsontext = "SMTP Error: The following recipients failed: [email protected]: : Recipient address rejected: Domain not found ↵{"success":false,"jsontext":"Error"}"

I also tried to add parameters when i create new PHPMailer:

$mail = new PHPMailer(false);

    $mail->isSMTP();
    $mail->SMTPDebug = false;
    $mail->do_debug = 0;

Found on this post: Disable PHPMailer error messages

Solution:

I found through functions this code:

if (!$mail->send()) {
        echo $mail->ErrorInfo;
        $estado = false;
    }

So i just commented fisrt lane to disable Errorinfo print :)


Solution

  • To get into that else you need to make sure that data.success contains a value that will not be considered true, and that's up to what you return from PHP, not what's in your JS.

    If you want to process the return value as JSON, you need to actually return JSON from your PHP - which your example jsontext is not.

    In your PHP it would be something like:

    if (!$mail->send()) {
        echo json_encode(['success' => false, 'jsontext' => $mail->ErrorInfo]);
    } else {
        echo json_encode(['success' => true, 'jsontext' => 'OK']);
    }