English spoken conference

Symfony 5: The Fast Track

A new book to learn about developing modern Symfony 5 applications.

Support this project

New in Symfony 4.3: Mime component

Contributed by
Fabien Potencier
in #30416.

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('[email protected]')
    ->to('[email protected]')
    ->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', ['[email protected]'])
    ->addMailboxListHeader('To', ['[email protected]'])
    ->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:

Twig Integration

One of the most important features of the Mime component is its deep integration with the Twig templating engine. The Symfony\Bridge\Twig\Mime\TemplatedEmail 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('[email protected]')
    ->to('[email protected]')
    // ...

    // 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:

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.

Comments

In the example :
```
$email = (new TemplatedEmail())
->from('[email protected]')
->fo('[email protected]')
// ...
```

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!

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.