dongzan9069 2016-05-26 07:17
浏览 76
已采纳

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

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条回答 默认 最新

  • du12197 2016-05-26 07:51
    关注

    In your error handler, the exception that your trying to catch is

    Swift_RfcComplianceException
    

    But the exception that is being thrown is

    Swift_TransportException
    

    To catch the transport exception, just add this to your error handler:

    if ($e instanceof \Swift_TransportException){
        return redirect($request->fullUrl())->with('error',"We have issue sending you an email");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧