Sending Emails with Mailer

New in version 4.3: The Mailer component was added in Symfony 4.3 and is currently experimental.The previous solution - Swift Mailer - is still valid: Swift Mailer.

Installation

Caution

The Mailer component is experimental in Symfony 4.3: some backwards compatibilitybreaks could occur before 4.4.

Symfony's Mailer & Mime components form a powerful systemfor creating and sending emails - complete with support for multipart messages, Twigintegration, CSS inlining, file attachments and a lot more. Get them installed with:

  1. $ composer require symfony/mailer

Transport Setup

Emails are delivered via a "transport". And without installing anything else, youcan deliver emails over smtp by configuring your .env file:

  1. # .env
  2. MAILER_DSN=smtp://user:[email protected]

Warning

If you are migrating from Swiftmailer (and the Swiftmailer bundle), bewarned that the DSN format is different.

Using a 3rd Party Transport

But an easier option is to send emails via a 3rd party provider. Mailer supportsseveral - install whichever you want:

ServiceInstall with
Amazon SEScomposer require symfony/amazon-mailer
Gmailcomposer require symfony/google-mailer
MailChimpcomposer require symfony/mailchimp-mailer
Mailguncomposer require symfony/mailgun-mailer
Postmarkcomposer require symfony/postmark-mailer
SendGridcomposer require symfony/sendgrid-mailer

Each library includes a Symfony Flex recipe that will addexample configuration to your .env file. For example, suppose you want touse SendGrid. First, install it:

  1. $ composer require symfony/sendgrid-mailer

You'll now have a new line in your .env file that you can uncomment:

  1. # .env
  2. SENDGRID_KEY=
  3. MAILER_DSN=smtp://$SENDGRID_KEY@sendgrid

The MAILERDSN isn't a _real SMTP address: it's a simple format that offloadsmost of the configuration work to mailer. The @sendgrid part of the addressactivates the SendGrid mailer library that you just installed, which knows allabout how to deliver messages to SendGrid.

The only part you need to change is to set SENDGRID_KEY to your key (in.env or .env.local).

Each transport will have different environment variables that the library will useto configure the actual address and authentication for delivery. Some also haveoptions that can be configured with query parameters on end of the MAILER_DSN -like ?region= for Amazon SES. Some transports support sending via httpor smtp - both work the same, but http is recommended when available.

Tip

Check the DSN formats for all supported providers.

Creating & Sending Messages

To send an email, autowire the mailer usingMailerInterface (service id mailer)and create an Email object:

  1. // src/Controller/MailerController.php
  2. namespace App\Controller;
  3.  
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\Mailer\MailerInterface;
  6. use Symfony\Component\Mime\Email;
  7.  
  8. class MailerController extends AbstractController
  9. {
  10. /**
  11. * @Route("/email")
  12. */
  13. public function sendEmail(MailerInterface $mailer)
  14. {
  15. $email = (new Email())
  16. ->from('[email protected]')
  17. ->to('[email protected]')
  18. //->cc('[email protected]')
  19. //->bcc('[email protected]')
  20. //->replyTo('[email protected]')
  21. //->priority(Email::PRIORITY_HIGH)
  22. ->subject('Time for Symfony Mailer!')
  23. ->text('Sending emails is fun again!')
  24. ->html('<p>See Twig integration for better HTML integration!</p>');
  25.  
  26. $mailer->send($email);
  27.  
  28. // ...
  29. }
  30. }

That's it! The message will be sent via whatever transport you configured.

Email Addresses

All the methods that require email addresses (from(), to(), etc.) acceptboth strings or address objects:

  1. // ...
  2. use Symfony\Component\Mime\Address;
  3. use Symfony\Component\Mime\NamedAddress;
  4.  
  5. $email = (new Email())
  6. // email address as a simple string
  7. ->from('[email protected]')
  8.  
  9. // email address as an object
  10. ->from(new Address('[email protected]'))
  11.  
  12. // email address as an object (email clients will display the name
  13. // instead of the email address)
  14. ->from(new NamedAddress('[email protected]', 'Fabien'))
  15.  
  16. // ...
  17. ;

Tip

Instead of calling ->from() every time you create a new email, you cancreate an event subscriber and listen to theMessageEvent::class event to set the same From email to all messages.

Multiple addresses are defined with the addXXX() methods:

  1. $email = (new Email())
  2. ->to('[email protected]')
  3. ->addTo('[email protected]')
  4. ->addTo('[email protected]')
  5.  
  6. // ...
  7. ;

Alternatively, you can pass multiple addresses to each method:

  1. $toAddresses = ['[email protected]', new Address('[email protected]')];
  2.  
  3. $email = (new Email())
  4. ->to(...$toAddresses)
  5. ->cc('[email protected]', '[email protected]')
  6.  
  7. // ...
  8. ;

Message Contents

The text and HTML contents of the email messages can be strings (usually theresult of rendering some template) or PHP resources:

  1. $email = (new Email())
  2. // ...
  3. // simple contents defined as a string
  4. ->text('Lorem ipsum...')
  5. ->html('<p>Lorem ipsum...</p>')
  6.  
  7. // attach a file stream
  8. ->text(fopen('/path/to/emails/user_signup.txt', 'r'))
  9. ->html(fopen('/path/to/emails/user_signup.html', 'r'))
  10. ;

Tip

You can also use Twig templates to render the HTML and text contents. Readthe Twig: HTML & CSS section later in this article tolearn more.

File Attachments

Use the attachFromPath() method to attach files that exist on your file system:

  1. $email = (new Email())
  2. // ...
  3. ->attachFromPath('/path/to/documents/terms-of-use.pdf')
  4. // optionally you can tell email clients to display a custom name for the file
  5. ->attachFromPath('/path/to/documents/privacy.pdf', 'Privacy Policy')
  6. // optionally you can provide an explicit MIME type (otherwise it's guessed)
  7. ->attachFromPath('/path/to/documents/contract.doc', 'Contract', 'application/msword')
  8. // you can also use an absolute URL if your PHP config allows getting URLs using fopen()
  9. // (this is not recommended because your application may or may not work depending on PHP config)
  10. ->attachFromPath('http://example.com/path/to/documents/contract.doc', 'Contract', 'application/msword')
  11. ;

Alternatively you can use the attach() method to attach contents from a stream:

  1. $email = (new Email())
  2. // ...
  3. ->attach(fopen('/path/to/documents/contract.doc', 'r'))
  4. ;

Embedding Images

If you want to display images inside your email, you must embed theminstead of adding them as attachments. When using Twig to render the emailcontents, as explained later in this article,the images are embedded automatically. Otherwise, you need to embed them manually.

First, use the embed() or embedFromPath() method to add an image from afile or stream:

  1. $email = (new Email())
  2. // ...
  3. // get the image contents from a PHP resource
  4. ->embed(fopen('/path/to/images/logo.png', 'r'), 'logo')
  5. // get the image contents from an existing file
  6. ->embedFromPath('/path/to/images/signature.gif', 'footer-signature')
  7. ;

The second optional argument of both methods is the image name ("Content-ID" inthe MIME standard). Its value is an arbitrary string used later to reference theimages inside the HTML contents:

  1. $email = (new Email())
  2. // ...
  3. ->embed(fopen('/path/to/images/logo.png', 'r'), 'logo')
  4. ->embedFromPath('/path/to/images/signature.gif', 'footer-signature')
  5. // reference images using the syntax 'cid:' + "image embed name"
  6. ->html('<img src="cid:logo"> ... <img src="cid:footer-signature"> ...')
  7. ;

Twig: HTML & CSS

The Mime component integrates with the Twig template engineto provide advanced features such as CSS style inlining and support for HTML/CSSframeworks to create complex HTML email messages. First, make sure Twig is installed:

  1. $ composer require symfony/twig-bundle

HTML Content

To define the contents of your email with Twig, use theTemplatedEmail class. This class extendsthe normal Email class but adds some new methodsfor Twig templates:

  1. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  2.  
  3. $email = (new TemplatedEmail())
  4. ->from('[email protected]')
  5. ->to(new NamedAddress('[email protected]', 'Ryan'))
  6. ->subject('Thanks for signing up!')
  7.  
  8. // path of the Twig template to render
  9. ->htmlTemplate('emails/signup.html.twig')
  10.  
  11. // pass variables (name => value) to the template
  12. ->context([
  13. 'expiration_date' => new \DateTime('+7 days'),
  14. 'username' => 'foo',
  15. ])
  16. ;

Then, create the template:

  1. {# templates/emails/signup.html.twig #}
  2. <h1>Welcome {{ email.toName }}!</h1>
  3.  
  4. <p>
  5. You signed up as {{ username }} the following email:
  6. </p>
  7. <p><code>{{ email.to[0].address }}</code></p>
  8.  
  9. <p>
  10. <a href="#">Click here to activate your account</a>
  11. (this link is valid until {{ expiration_date|date('F jS') }})
  12. </p>

The Twig template has access to any of the parameters passed in the context()method of the TemplatedEmail class and also to a special variable calledemail, which is an instance ofWrappedTemplatedEmail.

Text Content

When the text content of a TemplatedEmail is not explicitly defined, mailerwill generate it automatically by converting the HTML contents into text. If youhave league/html-to-markdown installed in your application,it uses that to turn HTML into Markdown (so the text email has some visual appeal).Otherwise, it applies the strip_tags PHP function to the originalHTML contents.

If you want to define the text content yourself, use the text() methodexplained in the previous sections or the textTemplate() method provided bythe TemplatedEmail class:

  1. + use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  2.  
  3. $email = (new TemplatedEmail())
  4. // ...
  5.  
  6. ->htmlTemplate('emails/signup.html.twig')
  7. + ->textTemplate('emails/signup.txt.twig')
  8. // ...
  9. ;

Embedding Images

Instead of dealing with the <img src="cid: …"> syntax explained in theprevious sections, when using Twig to render email contents you can refer toimage files as usual. First, to simplify things, define a Twig namespace calledimages that points to whatever directory your images are stored in:

  1. # config/packages/twig.yaml
  2. twig:
  3. # ...
  4.  
  5. paths:
  6. # point this wherever your images live
  7. '%kernel.project_dir%/assets/images': images

Now, use the special email.image() Twig helper to embed the images insidethe email contents:

  1. {# '@images/' refers to the Twig namespace defined earlier #}
  2. <img src="{{ email.image('@images/logo.png') }}" alt="Logo">
  3.  
  4. <h1>Welcome {{ email.toName }}!</h1>
  5. {# ... #}

Inlining CSS Styles

Designing the HTML contents of an email is very different from designing anormal HTML page. For starters, most email clients only support a subset of allCSS features. In addition, popular email clients like Gmail don't supportdefining styles inside <style> … </style> sections and you must inlineall the CSS styles.

CSS inlining means that every HTML tag must define a style attribute withall its CSS styles. This can make organizing your CSS a mess. That's why Twigprovides a CssInlinerExtension that automates everything for you. Installit with:

  1. $ composer require twig/cssinliner-extension

The extension is enabled automatically. To use this, wrap the entire templatewith the inline_css filter:

  1. {% apply inline_css %}
  2. <style>
  3. {# here, define your CSS styles as usual #}
  4. h1 {
  5. color: #333;
  6. }
  7. </style>
  8.  
  9. <h1>Welcome {{ email.toName }}!</h1>
  10. {# ... #}
  11. {% endapply %}

Using External CSS Files

You can also define CSS styles in external files and pass them asarguments to the filter:

  1. {% apply inline_css(source('@css/email.css')) %}
  2. <h1>Welcome {{ username }}!</h1>
  3. {# ... #}
  4. {% endapply %}

You can pass unlimited number of arguments to inline_css() to load multipleCSS files. For this example to work, you also need to define a new Twig namespacecalled css that points to the directory where email.css lives:

  1. # config/packages/twig.yaml
  2. twig:
  3. # ...
  4.  
  5. paths:
  6. # point this wherever your css files live
  7. '%kernel.project_dir%/assets/css': css

Rendering Markdown Content

Twig provides another extension called MarkdownExtension that lets youdefine the email contents using Markdown syntax. To use this, install theextension and a Markdown conversion library (the extension is compatible withseveral popular libraries):

  1. # instead of league/commonmark, you can also use erusev/parsedown or michelf/php-markdown
  2. $ composer require twig/markdown-extension league/commonmark

The extension adds a markdown filter, which you can use to convert parts orthe entire email contents from Markdown to HTML:

  1. {% apply markdown %}
  2. Welcome {{ email.toName }}!
  3. ===========================
  4.  
  5. You signed up to our site using the following email:
  6. `{{ email.to[0].address }}`
  7.  
  8. [Click here to activate your account]({{ url('...') }})
  9. {% endapply %}

Inky Email Templating Language

Creating beautifully designed emails that work on every email client is socomplex that there are HTML/CSS frameworks dedicated to that. One of the mostpopular frameworks is called Inky. It defines a syntax based on some simpletags which are later transformed into the real HTML code sent to users:

  1. <!-- a simplified example of the Inky syntax -->
  2. <container>
  3. <row>
  4. <columns>This is a column.</columns>
  5. </row>
  6. </container>

Twig provides integration with Inky via the InkyExtension. First, installthe extension in your application:

  1. $ composer require twig/inky-extension

The extension adds an inky filter, which can be used to convert parts or theentire email contents from Inky to HTML:

  1. {% apply inky %}
  2. <container>
  3. <row class="header">
  4. <columns>
  5. <spacer size="16"></spacer>
  6. <h1 class="text-center">Welcome {{ email.toName }}!</h1>
  7. </columns>
  8.  
  9. {# ... #}
  10. </row>
  11. </container>
  12. {% endapply %}

You can combine all filters to create complex email messages:

  1. {% apply inky|inline_css(source('@css/foundation-emails.css')) %}
  2. {# ... #}
  3. {% endapply %}

This makes use of the css Twig namespace we createdearlier. You could, for example, download the foundation-emails.css filedirectly from GitHub and save it in assets/css.

Sending Messages Async

When you call $mailer->send($email), the email is sent to the transport immediately.To improve performance, you can leverage Messenger to sendthe messages later via a Messenger transport.

Start by following the Messenger documentation and configuringa transport. Once everything is set up, when you call $mailer->send(), aSendEmailMessage message willbe dispatched through the default message bus (messenger.default_bus). Assumingyou have a transport called async, you can route the message there:

  • YAML
  1. # config/packages/messenger.yaml
  2. framework:
  3. messenger:
  4. transports:
  5. async: "%env(MESSENGER_TRANSPORT_DSN)%"
  6.  
  7. routing:
  8. 'Symfony\Component\Mailer\Messenger\SendEmailMessage': async
  • XML
  1. <!-- config/packages/messenger.xml -->
  2. <?xml version="1.0" encoding="UTF-8" ?>
  3. <container xmlns="http://symfony.com/schema/dic/services"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:framework="http://symfony.com/schema/dic/symfony"
  6. xsi:schemaLocation="http://symfony.com/schema/dic/services
  7. https://symfony.com/schema/dic/services/services-1.0.xsd
  8. http://symfony.com/schema/dic/symfony
  9. https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
  10.  
  11. <framework:config>
  12. <framework:messenger>
  13. <framework:routing message-class="Symfony\Component\Mailer\Messenger\SendEmailMessage">
  14. <framework:sender service="async"/>
  15. </framework:routing>
  16. </framework:messenger>
  17. </framework:config>
  18. </container>
  • PHP
  1. // config/packages/messenger.php
  2. $container->loadFromExtension('framework', [
  3. 'messenger' => [
  4. 'routing' => [
  5. 'Symfony\Component\Mailer\Messenger\SendEmailMessage' => 'async',
  6. ],
  7. ],
  8. ]);

Thanks to this, instead of being delivered immediately, messages will be sent tothe transport to be handled later (see Consuming Messages (Running the Worker)).

Development & Debugging

Disabling Delivery

While developing (or testing), you may want to disable delivery of messages entirely.You can do this by forcing Mailer to use the NullTransport in only the devenvironment:

  1. # config/packages/dev/mailer.yaml
  2. framework:
  3. mailer:
  4. dsn: 'smtp://null'

Note

If you're using Messenger and routing to a transport, the message will _still_be sent to that transport.

Always Send to the Same Address

Instead of disabling delivery entirely, you might want to always send emails toa specific address, instead of the real address. To do that, you can takeadvantage of the EnvelopeListener and register it only for the devenvironment:

  1. # config/services_dev.yaml
  2. services:
  3. mailer.dev.set_recipients:
  4. class: Symfony\Component\Mailer\EventListener\EnvelopeListener
  5. tags: ['kernel.event_subscriber']
  6. arguments:
  7. $sender: null
  8. $recipients: ['[email protected]']