Warning: You are browsing the documentation for Symfony 2.0, which is no longer maintained.

Read the updated version of this page for Symfony 5.3 (the current stable version).

How to test that an Email is sent in a functional Test

How to test that an Email is sent in a functional Test

Sending e-mails with Symfony2 is pretty straightforward thanks to the SwiftmailerBundle, which leverages the power of the Swiftmailer library.

To functionally test that an email was sent, and even assert the email subject, content or any other headers, you can use the Symfony2 Profiler.

Start with an easy controller action that sends an e-mail:

public function sendEmailAction($name)
    $message = \Swift_Message::newInstance()
        ->setSubject('Hello Email')
        ->setFrom('[email protected]')
        ->setTo('[email protected]')
        ->setBody('You should see me from the profiler!')


    return $this->render(...);


Don’t forget to enable the profiler as explained in How to use the Profiler in a Functional Test.

In your functional test, use the swiftmailer collector on the profiler to get information about the messages send on the previous request:

// src/Acme/DemoBundle/Tests/Controller/MailControllerTest.php
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MailControllerTest extends WebTestCase
    public function testMailIsSentAndContentIsOk()
        $client = static::createClient();
        $crawler = $client->request('POST', '/path/to/above/action');

        $mailCollector = $client->getProfile()->getCollector('swiftmailer');

        // Check that an e-mail was sent
        $this->assertEquals(1, $mailCollector->getMessageCount());

        $collectedMessages = $mailCollector->getMessages();
        $message = $collectedMessages[0];

        // Asserting e-mail data
        $this->assertInstanceOf('Swift_Message', $message);
        $this->assertEquals('Hello Email', $message->getSubject());
        $this->assertEquals('[email protected]', key($message->getFrom()));
        $this->assertEquals('[email protected]', key($message->getTo()));
            'You should see me from the profiler!',

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.