The Mailer Component

The Mailer component helps sending emails.

If you're using the Symfony Framework, read theSymfony Framework Mailer documentation.

New in version 4.3: The Mailer component was introduced in Symfony 4.3 and it's stillconsidered an experimental feature.

Installation

  1. $ composer require symfony/mailer

Note

If you install this component outside of a Symfony application, you mustrequire the vendor/autoload.php file in your code to enable the classautoloading mechanism provided by Composer. Readthis article for more details.

Usage

The Mailer component has two main classes: a Transport and the Mailer itself:

  1. use Symfony\Component\Mailer\Mailer;
  2. use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
  3.  
  4. $transport = new SmtpTransport('localhost');
  5. $mailer = new Mailer($transport);
  6. $mailer->send($email);

The $email object is created via the Mime component.

Transport

The only transport that comes pre-installed is SMTP.

Below is the list of other popular providers with built-in support:

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

For example, suppose you want to use Google's Gmail SMTP server. First, installit:

  1. $ composer require symfony/google-mailer

Then, use the SMTP Gmail transport:

  1. use Symfony\Component\Mailer\Bridge\Google\Smtp\GmailTransport;
  2.  
  3. $transport = new GmailTransport('user', 'pass');
  4. $mailer = new Mailer($transport);
  5. $mailer->send($email);

Each provider provides up to 3 transports: standard SMTP, HTTP (it uses theprovider's API but the body is created by the mailer component), API (it usesthe full API of the provider with no control over the body creation — featuresmight be limited as well).

The mailer component provides a convenient way to create a transport from aDSN:

  1. use Symfony\Component\Mailer\Transport;
  2.  
  3. $transport = Transport::fromDsn($dsn);

Where $dsn depends on the provider you want to use. For plain SMTP, usesmtp://user:pass@example.com or smtp://sendmail to use the sendmailbinary. For third-party providers, refers to the following table:

ProviderSMTPHTTPAPI
Amazon SESsmtp://ACCESS_KEY:SECRET_KEY@seshttp://ACCESS_KEY:SECRET_KEY@sesapi://ACCESS_KEY:SECRET_KEY@ses
Google Gmailsmtp://USERNAME:PASSWORD@gmailn/an/a
Mailchimp Mandrillsmtp://USERNAME:PASSWORD@mandrillhttp://KEY@mandrillapi://KEY@mandrill
Mailgunsmtp://USERNAME:PASSWORD@mailgunhttp://KEY:DOMAIN@mailgunapi://KEY:DOMAIN@mailgun
Postmarksmtp://ID:ID@postmarkn/aapi://KEY@postmark
Sendgridsmtp://apikey:KEY@sendgridn/aapi://KEY@sendgrid

High Availability

Symfony's mailer supports high availability via a technique called "failover"to ensure that emails are sent even if one mailer server fails .

A failover transport is configured with two or more transports joined by the|| operator:

  1. $dsn = 'api://[email protected] || smtp://[email protected]';

The mailer will start using the first transport. If the sending fails, themailer won't retry it with the other transports, but it will switch to the nexttransport automatically for the following deliveries.

Load Balancing

Symfony's mailer supports load balancing) via a technique called "round-robin"to distribute the mailing workload across multiple transports .

A round-robin transport is configured with two or more transports joined by the&& operator:

  1. $dsn = 'api://[email protected] && smtp://[email protected]'

The mailer will start using the first transport and if it fails, it will retrythe same delivery with the next transports until one of them succeeds (or untilall of them fail).

Sending emails asynchronously

If you want to send emails asynchronously, install the Messenger component.

  1. $ composer require symfony/messenger

Then, instantiate and pass a MessageBus as a second argument to Mailer:

  1. use Symfony\Component\Mailer\Mailer;
  2. use Symfony\Component\Mailer\Messenger\MessageHandler;
  3. use Symfony\Component\Mailer\Messenger\SendEmailMessage;
  4. use Symfony\Component\Mailer\SmtpEnvelope;
  5. use Symfony\Component\Mailer\Transport;
  6. use Symfony\Component\Messenger\Handler\HandlersLocator;
  7. use Symfony\Component\Messenger\MessageBus;
  8. use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware;
  9. use Symfony\Component\Mime\Address;
  10.  
  11. $dsn = 'change-dsn-accordingly';
  12.  
  13. $transport = Transport::fromDsn($dsn);
  14. $handler = new MessageHandler($transport);
  15.  
  16. $bus = new MessageBus([
  17. new HandleMessageMiddleware(new HandlersLocator([
  18. SendEmailMessage::class => [$handler],
  19. ])),
  20. ]);
  21.  
  22. $mailer = new Mailer($transport, $bus);
  23. $mailer->send($email);
  24.  
  25. // you can pass an optional Envelope
  26. $mailer->send($email, new SmtpEnvelope(
  27. new Address('[email protected]'),
  28. [
  29. new Address('[email protected]'),
  30. ]
  31. ));

Learn More

To learn more about how to use the mailer component, refer to theSymfony Framework Mailer documentation.