检测"交付给以下收件人永久失败"使用PHPMailer时出错

时间:2016-09-28 22:56:34

标签: php smtp phpmailer

我通过PHPMailer发送电子邮件,但有时我的电子邮件地址不是有效的电子邮件地址,所以我在我的Gmail帐户中看到一封错误的电子邮件"交付给以下收件人永久失败"。 我想使用PHPMailer捕获此错误并通知用户此错误。 当我试图发送电子邮件PHPMailer没有捕获此错误,我想它的一些配置问题。这是我的代码:

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());
        }

0 个答案:

没有答案