New in Symfony 4.3: Mime component
May 7, 2019 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
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.
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
```
$email = (new TemplatedEmail())
->from('fabien@symfony.com')
->fo('foo@example.com')
// ...
```
I think there is a little mistake. `->fo` is not rather `->to` ?