【问题标题】:Laravel 5 email error handeling when username or password is wrong用户名或密码错误时的 Laravel 5 电子邮件错误处理
【发布时间】: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;

标签: php email laravel-5 smtp


【解决方案1】:

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

Swift_RfcComplianceException

但是被抛出的异常是

Swift_TransportException

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

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

【讨论】:

  • 谢谢你成功了。你能告诉我如何阅读异常消息以显示给最终用户吗?
  • 太棒了,如果你想使用异常给你的信息,你可以这样做:return redirect($request->fullUrl())->with('error', $e->getMessage());
  • 感谢您的回复。你能告诉我如何在邮件函数中访问$to_email、$to_firstname 和$to_lastname 变量。对于以下代码,未识别变量并引发错误。 $to_email = 'to_email@gmail.com'; $to_firstname = 'Arun'; $to_lastname = 'Singh'; Mail::queue('/emails/test', array('firstname'=>'arun'), function($message){ $message->to($to_email, $to_firstname.' '.$to_lastname)->subject('Welcome '.$to_firstname.' to XYZ!'); }); 我正在从 db 中获取这些变量
  • 我不是 100% 确定邮件变量是如何工作的,请查看laravel.com/docs/5.1/mail#sending-mail,应该会有一些有用的提示。
【解决方案2】:

在我的 .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

谢谢。

【讨论】:

    猜你喜欢
    • 2017-01-20
    • 2016-06-10
    • 2017-01-07
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 2011-03-08
    • 1970-01-01
    相关资源
    最近更新 更多