php - 无法阻止在 Laravel 中向浏览器显示 SwiftMailer 异常

标签 php laravel swiftmailer

我在网站上使用 Laravel。我有一个页面,用户可以在其中与我联系。这会使用 SwiftMailer 包向我发送一封电子邮件。我刚刚注意到在尝试发送消息时出现以下错误

Fatal error: Uncaught exception 'Swift_TransportException'

异常从这里抛出 /Applications/MAMP/htdocs/yooies_site/bundles/swiftmailer/library/classes/Swift/Transport/Esmtp/AuthHandler.php 第 176 行

此位置的代码是:

  throw new Swift_TransportException(
'Failed to authenticate on SMTP server with username "' .
$this->_username . '" using ' . $count . ' possible authenticators'
);

错误的原因不用担心(我发现我已经更改了我的邮件密码并且忘了在我的网站电子邮件配置中更改)

我遇到的问题是我将 Laravel 设置为记录任何异常并且不在主页上显示它们。我的 error.php 配置文件中有以下设置

'detail' => false,
'log' => true,

那为什么我的网站在页面上显示异常而不是 500 错误?

这里有完整的堆栈跟踪(注意我已经删除了我的站点真实名称并替换为“站点”

Fatal error: Uncaught exception 'Swift_TransportException' 
with message 'Failed to authenticate on SMTP server with username "info+site.com" using 2 possible authenticators' 
in /Applications/MAMP/htdocs/site/bundles/swiftmailer/library/classes/Swift/Transport/Esmtp/AuthHandler.php:176 

Stack trace: 
#0 /Applications/MAMP/htdocs/site/bundles/swiftmailer/library/classes/Swift/Transport/EsmtpTransport.php(307): Swift_Transport_Esmtp_AuthHandler->afterEhlo(Object(Swift_SmtpTransport)) 
#1 /Applications/MAMP/htdocs/site/bundles/swiftmailer/library/classes/Swift/Transport/AbstractSmtpTransport.php(124): Swift_Transport_EsmtpTransport->_doHeloCommand() 
#2 /Applications/MAMP/htdocs/site/bundles/swiftmailer/library/classes/Swift/Mailer.php(79): Swift_Transport_AbstractSmtpTransport->start() 
#3 /Applications/MAMP/htdocs/site/application/libraries/personal/mailer.php(55): Swift_Mailer->send(Object(Swift_Message)) 
#4 /Applications/MAMP/htdocs/site/application/config/error.php(91): Personal\Mai in /Applications/MAMP/htdocs/site/bundles/swiftmailer/library/classes/Swift/Transport/Esmtp/AuthHandler.php on line 176

Laravel 版本为 L3。

此处捆绑 URL http://bundles.laravel.com/bundle/swiftmailer

最佳答案

Swiftmailer 不会触及任何负责显示错误的配置。对于生产服务器的建议之一是设置 display_errors = Offdisplay_startup_errors = Off,这可以在 php.ini 中设置, httpd.conf.htaccess.user.ini 等,甚至可以使用 ini_set()。您应该确保正确设置了这些指令。

我不熟悉 Laravel 如何处理异常和错误,但看起来 Swift_TransportException 是在 Laravel 的错误处理过程之外抛出的。您能指出异常是在应用程序流程中的哪个位置抛出的吗?也许是堆栈跟踪?

PS:提示:发送电子邮件之类的事情最好不要在请求期间完成。您可以将它卸载到某种队列,并让服务器上的另一个进程处理它。你可以看看Gearman为此。

更新

查看堆栈跟踪,Laravel 似乎确实捕获了异常,因为 Log::exception(); 被调用(在 application/config/error.php第 91 行)。但痕迹并没有揭示它是如何到达那里的。也许您可以在拥有 Xdebug 的开发机器上运行它安装。当异常被抛出时,Xdebug 会给你一个更精确的堆栈跟踪,以 public/index.php(请求开始的地方)结束。

关于php - 无法阻止在 Laravel 中向浏览器显示 SwiftMailer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21238985/

相关文章:

php - 仅允许登录用户查看托管在同一服务器和域上的外部 html 站点(在子文件夹中)

php - Apache 不会使用 PHP 连接到 SQL Server

php - 如何使用swift mailer读取模板?

php - Mysqli fetch array 警告,无法调试

php - 电子邮件帮助 - php mail() 或 SMTP

php - mysql从表中获取用户最高出价

php - 在 wordpress 核心中加载 css 和 js 文件

php - Laravel 5.1 强制非 WWW 和 HTTPS 不工作

php - octobercms 任务调度不工作

php - 在父 Controller 上调用方法?