简体   繁体   中英

How to send the notification as content as HTML with Notifier when using the email channel?

Is it possible to send and email as html with new Symfony Notifier service? How?

There's not much context to add to this simple question. Notifier class is a new service in Symfony 5 that implements sending notifications through different channels. It is currently marked as "experimental" so there's not a good documentation when it goes to specific needs.

Here you have some code sample:

$emailSubject = $this->getParameter('app.registration.email_subject_activation');
$emailContent = $this->render('email/user-activation.html.twig', ['activation_link' => $activationLink]);
$notification = (new Notification($emailSubject, ['email']))->content($emailContent);
$recipient    = new Recipient($user->getEmail());
$sentMessage  = $notifier->send($notification, $recipient);

The template content is seen as plain text, with html tags and all in the email.

If you want to send HTML content, you are better suited using Symfony Mailer directly instead of going through Notifier.

Notifier is an abstraction of many transports with vastly different capabilities. Eg not all chat messages treat HTML the same way. SMS supports 0 HTML. Etc.

Bearing that in mind, Notifier caters for a minimum denominator of plain text. Whatever you put in the content() will presented as plain text to the end user in any and all mediums. Remember that the same notification can be sent to multiple channels at the same time.

Just use Mailer directly, where you could simply do (following your example):

$email = new NotficationEmail();
$email->htmlTemplate('email/user-activation.html.twig')
    ->context(['activation_link' => $activationLink])
    ->to($user->getEmail())
    ->subject($ths->getParameter('app.registration.email_subject_activation'))
;

$mailer->send($email);

You can customize the notification messages and create a templated emails as you want. Please, check the following link

https://symfony.com/doc/current/notifier.html#customize-notification-messages

You can check the implementation in the Symfony security bundle:

Symfony\Component\Security\Http\LoginLink\LoginLinkNotification

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM