Using Swiftmailer in symfony


Symfony2 provides a default mailer component called swiftmailer. Its default configuration looks like this:

# app/config/config.yml
# ...
# Swiftmailer Configuration
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:     { type: memory }

The parameters are taken from the parameters.yml file. By default, swiftmailer sends from a locally configured SMTP host, so check your configuration and adapt it as required.

There is one configuration tweak added by default to config_dev.yml (so working only in the development mode):

#    delivery_address:

When uncommented and configured, it will cause swiftmailer to send any e-mail to this specific address, while in the development mode. This is a very useful option, as it allows us to test any e-mail to be sent to any user without sending the real e-mail to them.


This is only a basic list of options. The full list of options can be found in the Symfony2 reference documentation at

Let’s get back to our code. After we fetch our tasks, we need to send an e-mail for each of the tasks that are due today. To send an e-mail, we need to prepare a message as follows:

foreach ($tasks as $task) {
  $output->writeln('Sending email to: '. $task['email']);
  $message = Swift_Message::newInstance()
      ->setSubject('Task Reminder: '. $task['name'])
                ['task' => $task]
          ), 'text/html'

In the preceding fragment, we are setting up a basic message. What is worth noticing here is how we fetch the body. In our case, we are calling the templating service, and providing a Twig template (described later). We were using this service before within the controller. However, the controller class provides a $this->render() shortcut method; while in command you need to call the full service name.

When a method is prepared, we will send it as follows:

            try {
                $mailer = $this
            } catch (Exception $e) {
                $output->writeln('<error>'. $e->getMessage() .'<error>');

In case of error, an exception will be thrown and displayed in the console.

Now we need to add the promised template, as follows:

{# app/Resources/views/Email/reminder.html.twig #}
        <style type="text/css">
            body { font-family: Helvetica, Arial, sans-serif; }
            Hello {{ task.username }},</p>

        <p>This is a gently reminder about your task due today:</p>

            <strong>Your task details:</strong>

            <strong>{{ }}</strong><br />
            {{ task.notes }}
            Kind Regards,<br />
            Your To Do App

Note in the preceding code how we used arrays in Twig. In fact, there is no difference whether we use arrays or objects, we can access both of them in the same way. This is very convenient, as it allows the switching between objects and arrays with minimal effort when optimizing or refactoring code.

Finally, we can test our method, and see the e-mail in our mailbox. Remember to properly configure swiftmailer in config.yml and to enable the delivery address in the development mode.

Deven Rathore

Deven is an Entrepreneur, and Full-stack developer, Constantly learning and experiencing new things. He currently runs and

Published by
Deven Rathore

Recent Posts

Choose An App Development Company In 4 Easy Ways

The mobile app industry has been popular in recent years. Thanks to the continuing technological…

3 hours ago

3 Ways to Get the Most Out of Your University’s Virtual Computer Lab

IT is more important than ever in the world of higher education, and yet with…

3 hours ago

Top Tips for Learning Java Programming

If you’re here for the top tips, we assume you’re ahead of the “how to…

3 days ago

Neural Networks for Creating Blog Texts

The world is progressing at unprecedented rates at the current moment, especially in terms of…

4 days ago

Top 20 Opensource Python Tkinter Projects

This article will highlight the Top 20 Opensource Python Tkinter Projects which we believe will…

6 days ago

Beginners guide to Sneaker Proxies

With their numerous applications in streamlining the data flow, securing both the servers and the…

1 week ago