如何关闭SwiftMailer中的Smtp连接 [英] How to close Smtp connection in SwiftMailer

查看:181
本文介绍了如何关闭SwiftMailer中的Smtp连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SwiftMailer从齿轮工人进程发送电子邮件。我正在使用 Swift_SmtpTransport 类发送电子邮件。

I use SwiftMailer to send emails from a gearman worker process. I'm using the Swift_SmtpTransport class to send emails.

问题是如果这个工作进程保持空闲状态有时候,Sw​​iftMailer的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,这具体是这样的。

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.

推荐答案

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连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆