Creative Commons License
This work is licensed under a
Creative Commons
Attribution-Share Alike 3.0
Unported License.

Master Symfony2 fundamentals

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
trainings.sensiolabs.com

Discover the SensioLabs Support

Access to the SensioLabs Competency Center for an exclusive and tailor-made support on Symfony
sensiolabs.com

How to send an Email

Sending emails is a classic task for any web application and one that has special complications and potential pitfalls. Instead of recreating the wheel, one solution to send emails is to use the SwiftmailerBundle, which leverages the power of the Swift Mailer library.

Note

Don't forget to enable the bundle in your kernel before using it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public function registerBundles()
{
    $bundles = array(
        // ...

        new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
    );

    // ...
}

Configuration

Before using Swift Mailer, be sure to include its configuration. The only mandatory configuration parameter is transport:

  • YAML
    1
    2
    3
    4
    5
    6
    7
    8
    # app/config/config.yml
    swiftmailer:
        transport:  smtp
        encryption: ssl
        auth_mode:  login
        host:       smtp.gmail.com
        username:   your_username
        password:   your_password
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <!-- app/config/config.xml -->
    
    <!--
        xmlns:swiftmailer="http://symfony.com/schema/dic/swiftmailer"
        http://symfony.com/schema/dic/swiftmailer http://symfony.com/schema/dic/swiftmailer/swiftmailer-1.0.xsd
    -->
    
    <swiftmailer:config
        transport="smtp"
        encryption="ssl"
        auth-mode="login"
        host="smtp.gmail.com"
        username="your_username"
        password="your_password" />
    
  • PHP
    1
    2
    3
    4
    5
    6
    7
    8
    9
    // app/config/config.php
    $container->loadFromExtension('swiftmailer', array(
        'transport'  => "smtp",
        'encryption' => "ssl",
        'auth_mode'  => "login",
        'host'       => "smtp.gmail.com",
        'username'   => "your_username",
        'password'   => "your_password",
    ));
    

The majority of the Swift Mailer configuration deals with how the messages themselves should be delivered.

The following configuration attributes are available:

  • transport (smtp, mail, sendmail, or gmail)
  • username
  • password
  • host
  • port
  • encryption (tls, or ssl)
  • auth_mode (plain, login, or cram-md5)
  • spool
    • type (how to queue the messages, file or memory is supported, see How to Spool Emails)
    • path (where to store the messages)
  • delivery_address (an email address where to send ALL emails)
  • disable_delivery (set to true to disable delivery completely)

Sending Emails

The Swift Mailer library works by creating, configuring and then sending Swift_Message objects. The "mailer" is responsible for the actual delivery of the message and is accessible via the mailer service. Overall, sending an email is pretty straightforward:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public function indexAction($name)
{
    $message = \Swift_Message::newInstance()
        ->setSubject('Hello Email')
        ->setFrom('send@example.com')
        ->setTo('recipient@example.com')
        ->setBody(
            $this->renderView(
                'HelloBundle:Hello:email.txt.twig',
                array('name' => $name)
            )
        )
    ;
    $this->get('mailer')->send($message);

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

To keep things decoupled, the email body has been stored in a template and rendered with the renderView() method.

The $message object supports many more options, such as including attachments, adding HTML content, and much more. Fortunately, Swift Mailer covers the topic of Creating Messages in great detail in its documentation.

Tip

Several other cookbook articles are available related to sending emails in Symfony2: