Laravel 5 用户名或密码错误时的邮件错误处理

Laravel 5 email error handeling when username or password is wrong

我第一次使用 smtp 发送邮件 Laravel。

我已经配置了我的 gmail 详细信息,一切正常,我正在将邮件发送到我的收件箱。

但是我想知道如何处理 error/exception 当提供的用户名或密码等 gmail 信息错误时。

现在,当我提供错误的密码时,它会将我带到错误屏幕并显示以下消息:

Swift_TransportException in AbstractSmtpTransport.php line 383: Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/answer/14257 f191sm3044706pfa.26 - gsmtp"

我尝试在 app\Exceptions\handler.php 中添加一些代码,如下所示

public function render($request, Exception $e)
{
    //This is my code
    if ($e instanceof Swift_RfcComplianceException){
        return redirect($request->fullUrl())->with('error',"We have issue sending you an email");
    }
    return parent::render($request, $e);
}

我在 .env 文件中的 smtp 详细信息是:(工作正常)

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_ADDRESS=myemail@gmail.com
MAIL_NAME=Arun Singh
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=mypassword

发送邮件的代码片段:(工作正常)

Mail::queue('/emails/test', array('firstname'=>'arun'), function($message){
    $to_email = 'toemail@gmail.com';
    $to_firstname = 'Sanju';
    $to_lastname = 'Singh';
    $message->to($to_email, $to_firstname.' '.$to_lastname)->subject('Welcome to the XYZ!');
})

感谢您的帮助。

在您的错误处理程序中,您尝试捕获的异常是

Swift_RfcComplianceException

但是抛出的异常是

Swift_TransportException

要捕获传输异常,只需将其添加到您的错误处理程序中:

if ($e instanceof \Swift_TransportException){
    return redirect($request->fullUrl())->with('error',"We have issue sending you an email");
}

在我的 .env 文件中

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME= angambameetei93@gmail.com
MAIL_PASSWORD= xxxxxxxxxxxxxx
MAIL_ENCRYPTION=tls

我得到了同样的错误,但很长一段时间后我通过

修复了它
https://myaccount.google.com/lesssecureapps

在这里,安全性较低的应用访问权限必须开启

Allow less secure apps: ON

谢谢。