fmarinheiro
fmarinheiro

Reputation: 87

Symfony mailer: Swift_TransportException between message sending

On a current project which I'm currently working, i have a symfony task that runs some mass data insertion to database and runs it for at least half an hour. When the task starts a mail notification is sent correctly, the problem is that at the of the task execution we can't send another mail to notify about the end of processing.

The mailer factory is currently configured with the spool delivery strategy but, in this specific situation, we desire to fire a notification immediately, using the sendNextImmediately() method.

I'm are getting the exception:

[Swift_TransportException]
Expected response code 250 but got code "451", with message "451 4.4.2 Timeout - closing connection. 74sm1186065wem.17 "

and the flowing error on php log file:

Warning: fwrite(): SSL: Broken pipe in /var/www/project/lib/vendor/symfony/lib/vendor/swiftmailer/classes/Swift/Transport/StreamBuffer.php on line 209

Can anyone give some help? Is there any way that i can perhaps refresh symfony mailer to establish a new connection?

Upvotes: 4

Views: 4297

Answers (3)

fmarinheiro
fmarinheiro

Reputation: 87

For Symfony1 Users

My guess was that the connection was being hold for too long (with no activity at all), causing an ssl connection timeout.

For now, the problem can be solved by stopping the Swift_Transport instance and starting it again explicitly, just before sending the second message.

Here is the code:

$this->getMailer()->getRealtimeTransport()->stop();
$this->getMailer()->getRealtimeTransport()->start();
$this->getMailer()->sendNextImmediately()->send($message);

Upvotes: 1

Greg Motyl
Greg Motyl

Reputation: 2555

I had exactly the same problem and above solutions were very helpful, but there is one thing I had to do differently: order.

$this->getMailer()->sendNextImmediately()->send($message);
$this->getMailer()->getRealtimeTransport()->stop();

It didn't worked for me if I tried to stop Transport before sending message (connection timeout was already hanging). Also You don't need to run getRealtimeTransport()->start() - it will be started automatically.

Upvotes: 0

stoefln
stoefln

Reputation: 14585

Doing a Symfony2 project, I ran across this failure too. We were using a permanently running php script, which produced the error.

We figured out that following code does the job:

private function sendEmailMessage($renderedTemplate, $subject, $toEmail)
    {
        $mailer = $this->getContainer()->get('mailer');
        /* @var $mailer \Swift_Mailer */
        if(!$mailer->getTransport()->isStarted()){
            $mailer->getTransport()->start();
        }
        $sendException = null;
        /* @var $message \Swift_Message */
        $message = \Swift_Message::newInstance()
            ->setSubject($subject)
            ->setFrom($this->getContainer()->getParameter('email_from'))
            ->setTo($toEmail)
            ->setBody($renderedTemplate);


        $mailer->send($message);
        $mailer->getTransport()->stop();
        //throw $sendException;
    }

Upvotes: 5

Related Questions