How to close Smtp connection in SwiftMailer

前端 未结 5 1357
借酒劲吻你
借酒劲吻你 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:18

    When pipe is broken $mailer->getTransport()->stop() will fail as well. And due to this error transport cannot be stopped. The workaround is

    // Let's try to send an email.
    $tries = 3;
    while ($tries--) {
        try {
            $sent = $this->mailer->send($message);
            break;
        } catch (\Exception $e) {
            // Connection problems
            // @see https://github.com/swiftmailer/swiftmailer/issues/490
            try {
                // Try to stop
                $this->mailer->getTransport()->stop();
            } catch (\Exception $e) {
                // Got Exception while stopping transport.
                // We have to set _started to 'false' manually, because due to an exception it is 'true' now.
                $t = $this->mailer->getTransport();
                $reflection = new \ReflectionClass($t);
                $prop = $reflection->getProperty('_started');
                $prop->setAccessible(true);
                $prop->setValue($t, false);
                $prop->setAccessible(false);
            }
        }
    }
    

提交回复
热议问题