How to close Smtp connection in SwiftMailer(如何在 SwiftMailer 中关闭 SMTP 连接)
问题描述
我使用 SwiftMailer 从 gearman 工作进程发送电子邮件.我正在使用 Swift_SmtpTransport
类发送电子邮件.
I use SwiftMailer to send emails from a gearman worker process. I'm using the Swift_SmtpTransport
class to send emails.
问题是,如果这个工作进程保持空闲一段时间,SwiftMailer smtp 连接就会超时.现在,当下一个作业到达时,SwiftMailer 无法发送电子邮件,因为连接已超时.
The problem is that if this worker process stays idle for sometime, the SwiftMailer smtp connection times out. Now when the next job arrives, SwiftMailer fails to send emails as the connection has been timed out.
理想情况下,我希望在每次作业后关闭 smtp 连接.我无法在专门执行此操作的类中找到 api.unset()
对象也不起作用,因为这是一个静态类.
Ideally, I would want to close the smtp connection after every job. I'm unable to locate a api in the class which does this specifically. Neither does unset()
object works since this is a static class.
推荐答案
有一个粗鲁的选择:明确停止传输.在随后调用 sendMail 方法时,SwiftMailer 将检查传输是否已启动(现在未启动)并再次启动它.IMNSHO,SwiftMailer 应该拦截 SMTP 超时并自动重新连接.但是,目前,这是解决方法:
There is a rude option: stop the transport explicitly. On subsequent calls of the method sendMail, SwiftMailer will check whether the transport is up (it is not, now) and start it again. IMNSHO, SwiftMailer should intercept the SMTP timeout and reconnect automatically.But, for now, this is the workaround:
function sendMail($your_args) {
try{
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john@doe.com' => 'John Doe'))
->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
->setBody('Here is the message itself');
$result = $mailer->send($message);
$mailer->getTransport()->stop();
} catch (Swift_TransportException $e) {
//this should be caught to understand if the issue is on transport
} catch (Exception $e) {
//something else happened
}
}
这篇关于如何在 SwiftMailer 中关闭 SMTP 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 SwiftMailer 中关闭 SMTP 连接


- PHP Count 布尔数组中真值的数量 2021-01-01
- Laravel 仓库 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01