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: [email protected]

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.

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'])
      ->setFrom([email protected]')
                ['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.

Recommended :  Easily monitor your Campaign in laravel

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.

Recommended :  A simple polymorphic relationship in laravel

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.

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest