Symfony is another popular PHP framework and a great toolset for web development. Initially, its mailer feature was based on the Swift Mailer library, also widely used for sending emails from PHP applications.
In the Symfony 4.3 release, the new Mailer component was introduced along with the Mime component. This way, Symfony now has an internal email sending system with a wide set of options:
- direct integration with the most popular email sending providers
- asynchronous email sending
- signing and encrypting messages
- Twig templates
- CSS inliner
- file attachments
The Mailer component was added to the 4.3 release as an experiment. It has proved successful, and has been constantly improved and updated. In this post, we use Symfony 5.1.
If you would like to master sending messages with Swift Mailer, then follow our Sending emails with Swift Mailer guide.
We assume that you already work with Symfony and are acquainted with its main components and principles.
What we will be doing in this post with Symfony Mailer
- Install Mailer and Mime components.
- Define mail transport (SMTP) and set up Mailtrap for our email experiments.
- Review the code for creating a new message and sending it to several recipients.
- Add email content as pure HTML, with an embedded image, and a file attachment.
- Refer to the Twig templates and go over its main capabilities.
- Craft the full email template and send it to Mailtrap to check how it works.
- List the main sending options and their configurations (Gmail, Amazon SES, Sendgrid, etc.)
Mailer component
In Symfony, message creation and sending can be done with the help of two components: Mailer and Mime. Let’s start by installing them both: