Symfony provides more than 60 decoupled components to solve common needs of web and console applications. New Symfony versions usually introduce new components and Symfony 4.3 will be no exception. In this blog post you'll learn about one of those new components: Mime component.
This component helps you create and manipulate the MIME messages used to send emails and provides utilities related to MIME types. The full MIME standard (Multipurpose Internet Mail Extensions) is a set of standards that define additional capabilities for the original text-based emails (such as rich HTML formatting and file attachments).
The Mime component abstracts all that complexity to provide two ways of creating
MIME messages. The first one is a high-level API based on the
Symfony\Component\Mime\Email
class to quickly create email messages
with all the common features:
1 2 3 4 5 6 7 8 9
use Symfony\Component\Mime\Email;
$email = (new Email())
->from('fabien@symfony.com')
->to('foo@example.com')
->subject('Important Notification')
->text('Lorem ipsum...')
->html('<h1>Lorem ipsum</h1> <p>...</p>')
;
The other way to create MIME messages is a low-level API based on the
Symfony\Component\Mime\Message
class which gives you absolute
control over every single part of the email message:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
use Symfony\Component\Mime\Header\Headers;
use Symfony\Component\Mime\Message;
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
use Symfony\Component\Mime\Part\TextPart;
$headers = (new Headers())
->addMailboxListHeader('From', ['fabien@symfony.com'])
->addMailboxListHeader('To', ['foo@example.com'])
->addTextHeader('Subject', 'Important Notification')
;
$textContent = new TextPart('Lorem ipsum...');
$htmlContent = new TextPart('<h1>Lorem ipsum</h1> <p>...</p>', 'html');
$body = new AlternativePart($textContent, $htmlContent);
$email = new Message($headers, $body);
The Mime component provides many other utilities to create email messages:
- Flexible definition of email addresses using strings, objects, named addresses, etc.
- Image embedding, which is needed when sending complex HTML emails.
- File attachments, using physical files and/or PHP resources.
Twig Integration
One of the most important features of the Mime component is its deep integration
with the Twig templating engine. The Symfony
class for example lets you render a Twig template to generate the email contents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
$email = (new TemplatedEmail())
->from('fabien@symfony.com')
->to('foo@example.com')
// ...
// this method defines the path of the Twig template to render
->htmlTemplate('messages/user/signup.html.twig')
// this method defines the parameters (name => value) passed to templates
->context([
'expiration_date' => new \DateTime('+7 days'),
'username' => 'foo',
])
;
Using Twig also enables the following features of the Mime component:
- Simpler image embedding.
- CSS style inlining, which is needed because some popular email clients don't
support CSS styles defined inside
<style> ... </style>
sections. - Markdown rendering in case you want to define the contents of your emails using the popular Markdown syntax.
- Inky templating language support, which is one of the most popular languages used to create email messages with responsive HTML designs.
The Mime component provides everything you might need to create any kind of email message ... but it doesn't actually send those messages. Emails are sent using another new component called Mailer which we'll be presented in this "New in Symfony 4.3" series soon.
In the example :
I think there is a little mistake.
->fo
is not rather->to
?Can you explain the reason why this component was added and the advantages over SwiftMailer?
@Jáchym Toušek you can check this PR : https://github.com/symfony/symfony/pull/30416
@Jáchym Toušek It started as an evolution of Swiftmailer, but the more I worked on the refactoring, the more I realized that I was building something different.
@Kevin, thanks for reporting this error. The blog post has been updated. Cheers!
all github links are currently broken, e.g. https://github.com/symfony/symfony/blob/4.2/src/Symfony/Component/Mime/Email.php. the "4.2" needs to be replaced with "4.3"
@Georg thanks for the heads up. I've just replaced the failing links by the FQCN of the classes.
A question about this component: is it possible to manipulate existing raw mime emails with it? My use case would be to process emails forwarded from AWS SNS via webhooks. Thanks!