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.4: Notification Emails

Contributed by
Fabien Potencier
in #33605.

The Mime component was introduced in Symfony 4.3 to help you create email messages. In Symfony 4.4 we've improved it with notification emails. These emails are standardized messages used to send notifications to yourself (e.g. a new user signed up, some invoice was paid, etc.)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
use Symfony\Bridge\Twig\Mime\NotificationEmail;

$email = (new NotificationEmail())
    ->from('[email protected]')
    ->to('[email protected]')
    ->subject('My first notification email via Symfony')
    ->markdown(<<<EOF
        There is a **problem** on your website, you should investigate it
        right now. Or just wait, the problem might solves itself automatically,
        we never know.
        EOF
    )
    ->action('More info?', 'https://example.com/')
    ->importance(NotificationEmail::IMPORTANCE_HIGH)
;

Now, use the Mailer component to actually send this message and you'll see something like this in your email client:

A notification email created with Symfony Mime component

Notification emails are already styled with a responsive design based on Foundation for Emails 2, but you can override the theme globally or per email.

Comments

Awesome !
What is exactly the difference between Notification Emails and just sending an Email?
@Thijm under the hood they are the same: just email messages. But "notification emails" come with a predesigned style which is responsive (a normal email is just plain text or raw HTML tags and you need to spend some time styling them).

The "notification emails" also display their priority in a visible way and contain useful shortcuts such as the "action()" method to quickly add clickable buttons in the email message.

In any case, as mentioned above, these emails are intended to send notification to yourself, not to your customers.
Why the focus on sending notifications to yourself?

This looks fine for stuff like "You've unlocked the first news comment in 2019 badge on symfony.com" or "Your reservation for the SymfonyCon will expire in 2 weeks." which could be send to customers, too?
Not to nitpick but the heredoc syntax in the example requires at least 7.3.x to run. It will fail under 7.1.3. Need to move the EOF all they way to the left which in turn can introduce some extra white space.
@Art Hundiak it's not nitpicking. You are right. The example uses the great new flexible HEREDOC syntax introduced in PHP 7.3 (which was released on December 2018).
Login with SymfonyConnect to post a comment