The stable version of Symfony 4.3 was released on May 30 2019, but there are still some new features we haven't talked about. In this article you'll learn about the Mailer component, the third component added by Symfony 4.3 (after Mime component and HttpClient component).
The Mime component allows you to create email messages, but to actually send them, you need to use the Mailer component. Emails are delivered via a "transport", which can be a local SMTP server or a third-party mailing service.
Out of the box this component provides support for the most popular services: Amazon SES, MailChimp, Mailgun, Gmail, Postmark and SendGrid. They are installed separately, so if your app uses for example Amazon SES, run this command:
1
$ composer require symfony/amazon-mailer
This will add some environment variables in your .env
file where you can
configure the specific service you are using:
1 2 3 4
# .env
AWS_ACCESS_KEY=...
AWS_SECRET_KEY=...
MAILER_DSN=smtp://$AWS_ACCESS_KEY:$AWS_SECRET_KEY@ses
That's all. You can now inject the mailer service in any service or controller
by type-hinting a constructor argument with the MailerInterface
class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
class SomeService
{
private $mailer;
public function __construct(MailerInterface $mailer)
{
$this->mailer = $mailer;
}
public function sendNotification()
{
$email = (new Email())
->from('hello@example.com')
->to('you@example.com')
->subject('Time for Symfony Mailer!')
->text('Sending emails is fun again!')
->html('<p>See Twig integration for better HTML integration!</p>');
$this->mailer->send($email);
}
}
When you call $this->mailer->send($email)
, the email message is sent to the
transport immediately. To improve performance, you can leverage the
Messenger component to send the messages later via a Messenger transport.
Read the Sending Messages Async section in the Mailer docs to learn more
about this.
This is not clearly indicated, but I suppose this new componant is standalone and doesn't require Swiftmailer? This means Swiftmailer is going to die?
@Sébastien this component is going to replace Swiftmailer ... but not yet. We want to test the new Mailer component well in Symfony 4.3 and polish/tweak the entire experience so we can replace/deprecate Swiftmailer in future versions (ideally Symfony 4.4/5.0 to be released in November 2019).
Great, Thanks! Where can I learn more about the twig integration?
@Daniel the Twig integration and all the other mailer features are explained here: https://symfony.com/doc/current/mailer.html
awesome....
Great! Will the component support Mailjet?
I had to start a small service for a project a few days ago. I created it with Symfony 4.3 and that's how some how I got everything working in a couple of days, partly thanks to this new Mailer component and its integration with Mandrill / MailChimp API integration. Thank you for this great new addition to the Symfony ecosystem :)
Great news from Symfony, of course! I wonder if you have planned to release a library to retrieve emails from any inbox (gmail, ipage and so on).
Best regards.
I am also looking for retrieving emails and parse them into data objects to store them in a database or so
Hi, how to configure monolog with this new component ?
Thank you for this! We were just given a PHP project that doesn't use Symfony and I was dreading trying to figure out how to deal with usernames, passwords and encryption for
mail()
without resorting to Pear.Transport::fromDsn()
added to anew Mailer()
did the trick!