How to close Smtp connection in SwiftMailer

前端 未结 5 1383
借酒劲吻你
借酒劲吻你 2020-12-31 04:37

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

The problem is that if this wor

5条回答
  •  情深已故
    2020-12-31 05:31

    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  
        }
    
    }
    

提交回复
热议问题