Question

I've got an odd problem that I can't put my finger on.

In my controller, I'm sending an email using swiftmailer, setting a flash message using the session flashbag, and redirecting to another page.

public function testAction() {
    $this->get('session')->getFlashBag()->add('info', 'test flash message');

    $message = new \Swift_Message('test', 'test');
    $message->setTo('email@example.com');
    $message->setFrom('email@example.com');

    $this->get('mailer')->send($message);

    return $this->redirect($this->generateUrl('home_page'));
}

After the redirect, I expect to see the flash message, but it seems that something happens when the mail is sent that makes the flash message delayed by one request. That is, I don't see the flash message when I get to 'home_page', but I do if I reload the page after the redirect.

If I comment out the send() call or even set disable_delivery: true in the swiftmail configuration, the flash message works as expected.

I thought I might have found a bug, but when I created a new project (symfony/framework-standard-edition 2.4.1) and tried this, it works even when I send mail. I've checked to make sure I'm using the same releases for swiftmailer, symfony, etc. So, I'm sure it's something with my project, but I am out of ideas of where to go from here.

I'm using PHP 5.5.7, Symfony 2.4.1.

Thanks for your time

Was it helpful?

Solution

I believe there's a bug when using PdoSessionHandler for storing sessions - if you use native session storage the problem goes away and the flash message works as expected with SwiftMailer.

The flash messages (app.session.flashbag) rely on sessions to store the data between page loads.

OTHER TIPS

A quick fix is to disable spooling for SwiftMailer. I am still uncertain whether this is the actual solution, but it seems to work. By disabling spooling, the mailer will block execution while sending the mail.

I commented out the line that enables spooling:

#spool:     { type: memory }

By default, the mailer will not use spooling but send the mail right away: http://symfony.com/doc/master/cookbook/email/spool.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top