New in Symfony 4.4: Notification Emails
October 29, 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.
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('fabien@example.com')
->to('fabien@example.org')
->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:
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.
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.
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).
It should be noted that PHP 7.1 has reached its end of life and should not be used for critical applications as it will not receive security updates. I doubt the team will bother to put the effort in to support unsupported PHP versions. https://www.php.net/supported-versions.php
Hi! I was wondering to try this new great feature, but I got error messages requiring different twig packages. After installing them, I get the following error: Unknown "inky_to_html" filter
Is there something I am doing wrong?
Thanks!
hi ! https://symfony.com/doc/current/mailer.html#inky-email-templating-language this link points to the doc that explains how this very practical framework works
I cannot get the example to work with markdown. I'm running it in standard PHP code (not Symfony framework) and am getting this error:
PHP Fatal error: Uncaught Symfony\Component\Mime\Exception\LogicException: A message must have a text or an HTML part or attachments. in /home/avrsites/websites/azvr.com/web/vendor/symfony/mime/Email.php:405
It works fine if I use ->html() or ->text(), but I was hoping I could use ->markdown()
I've tried installing all the suggested extra packages, but I still get the error.
Hi,
I'm trying to override globally the theme of notification email without success... Any clue ?
thanks