dongzan9069
dongzan9069
2016-05-26 07:17

用户名或密码错误时,Laravel 5电子邮件错误处理

  • smtp
  • php
  • email

I'm working first time on sending mail using smtp in Laravel.

I have configured my gmail details and every thing is working fine and I'm getting mail to my inbox.

But I want to know how to handle error/exception when provided gmail information like Username or Password is wrong.

Right now when I provide a wrong password it is taking me to error screen with following message:

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"

I have tried adding some code in app\Exceptions\handler.php as below

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

My smtp details in .env file are: (Working fine)

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

Code snippet for sending mail: (Working fine)

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!');
})

Thank you for your help.

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答