如何关闭SwiftMailer中的Smtp连接

Nan*_*nds 16 php email smtp worker swiftmailer

我使用SwiftMailer从一个齿轮工人进程发送电子邮件.我正在使用该Swift_SmtpTransport课程发送电子邮件.

问题是如果此工作进程暂时保持空闲状态,则SwiftMailer smtp连接会超时.现在,当下一个作业到来时,SwiftMailer无法发送电子邮件,因为连接已超时.

理想情况下,我想在每个工作后关闭smtp连接.我无法在课堂上找到一个专门做这个的api.unset()对象也不起作用,因为这是一个静态类.

cer*_*nio 11

有一个粗鲁的选择:明确停止运输.在随后调用sendMail方法时,SwiftMailer将检查传输是否已启动(现在不是)并再次启动它.IMNSHO,SwiftMailer应该拦截SMTP超时并自动重新连接.但是,现在,这是解决方法:

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  
    }

}
Run Code Online (Sandbox Code Playgroud)


Álv*_*lez 10

我在一个循环中发送邮件,我正在捕捉Swift_TransportException并创建一个新的实例,Swift_Mailer但它不是正确的解决方案:问题是传输,而不是邮件.解决方案是发出一个显式调用Swift_SmtpTransport::stop():

foreach($recipients as $to => $body){
    try{
        $message->setTo($to);
        $message->setBody(body);
        $mailer->send($message);
    }catch(Swift_TransportException $e){
        $mailer->getTransport()->stop();
        sleep(10); // Just in case ;-)
    }
}
Run Code Online (Sandbox Code Playgroud)

这样,Swift检测到邮件程序已停止并自动启动,因此可以正确地从通信错误中恢复.


Mic*_*ins 0

我正在使用 Swiftmailer 和 AWS SES 在无限循环中运行工作程序,但出现错误:

Expected response code 250 but got code "421", with message "421 Timeout waiting for data from client.
Run Code Online (Sandbox Code Playgroud)

我的脚本的解决方案:

$love = true;
while($love) {
    $message = Message::to($record->to)
        ->from(array('no-reply@clouddueling.com' => $user->name()))
        ->reply(array($user->email => $user->name()))
        ->subject($record->subject)
        ->body($body->value)
        ->html(true)
        ->send();

    if (! $message->was_sent())
        throw new Swift_TransportException($errstr . ': ' . $errno);
}
Run Code Online (Sandbox Code Playgroud)

  • 这是一个答案还是一个问题? (4认同)
  • 这个答案可以用一个解释。您的意思是这样的错误不是抛出`Swift_TransportException`,而是明确地解决了问题? (3认同)