Detect “Delivery to the following recipient failed permanently” error using PHPMailer

By : dasdasd
Source: Stackoverflow.com
Question!

Im sending email through PHPMailer but sometimes the email addresses that I have are not valid email addresses so I see in my gmail account an email with the error "Delivery to the following recipient failed permanently". I want to catch this error using PHPMailer and to notify the user about this error. When Im trying to send the email PHPMailer does not catch this error, I guess that its some config issue. This is my code:

if(empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL))
                    return BasicServer::serverResponse(Errors::NOT_VALID_PASSWORD_EMAIL_RESTORE, "User email is not valid");


       $mailer = new PHPMailer(true);
        $mailer->isSMTP();

        try {

            $mailer->CharSet        = 'UTF-8';
            $mailer->FromName       = "soInFit";
            $mailer->Host           = "xxxxxx"; // SMTP server
            debug information (for testing)
            $mailer->SMTPAuth       = true;                  // enable SMTP authentication
            $mailer->SMTPSecure     = "ssl";                 // sets the prefix to the servier     
            $mailer->Host           = "smtp.gmail.com";      // sets GMAIL as the SMTP server
            $mailer->Port           = 465;                   // set the SMTP port for the GMAIL server
            $mailer->Username       = "xxxxxx";  // GMAIL username
            $mailer->Password       = 'xxxxx';            // GMAIL password
            $mailer->Subject        = $subject;
            $mailer->Body           = $message;
            $mailer->IsHTML(true); 

            $mailer->addAddress($email);
            if(!$mailer->Send())
                return BasicServer::serverResponse(Errors::UPDATE_ERROR, $mailer->ErrorInfo);

        } catch (phpmailerException $ex) {

            return BasicServer::serverResponse(Errors::UPDATE_ERROR, $ex->errorMessage());
        }
        catch (Exception $e) {
            return BasicServer::serverResponse(Errors::UPDATE_ERROR, $e->errorMessage());
        }
By : dasdasd


This video can help you solving your question :)
By: admin