【发布时间】:2016-09-24 01:18:50
【问题描述】:
我第一次在 Laravel 中使用 smtp 发送邮件。
我已经配置了我的 gmail 详细信息,并且一切正常,我正在将邮件发送到我的收件箱。
但我想知道当提供的 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
发送邮件的代码 sn-p:(工作正常)
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_TransportException,你应该在你的自定义异常渲染器中检查if ($e instanceof Swift_TransportException) -
谢谢你成功了。你能告诉我如何阅读异常消息以显示给最终用户吗?
-
可以,可以通过Exception对象的getMessage函数访问异常消息:
$e->getMessage() -
是的,我现在可以阅读该消息,但它仍然显示
Call to a member function send() on a non-object。现在修复这个。我正在尝试做:$status = Mail::queue('/emails/test', array('firstname'=>'arun'), function($message){ $to_email = 'arunsingh0430@gmail.com'; $to_firstname = 'Arun'; $to_lastname = 'Singh'; $message->to($to_email, $to_firstname.' '.$to_lastname)->subject('Welcome to the Adelphi!'); }); echo $status;