From eab240e708d400da1c731fe448c31ee014b953b1 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sat, 21 Sep 2019 11:58:37 +0200 Subject: [PATCH] Simplified the mailer docs about a global from address --- mailer.rst | 40 ++++++---------------------------------- 1 file changed, 6 insertions(+), 34 deletions(-) diff --git a/mailer.rst b/mailer.rst index adb367f1996..55096dd62aa 100644 --- a/mailer.rst +++ b/mailer.rst @@ -153,6 +153,12 @@ both strings or address objects:: // ... ; +.. tip:: + + Instead of calling ``->from()`` *every* time you create a new email, you can + create an :doc:`event subscriber ` and listen to the + ``MessageEvent::class`` event to set the same ``From`` email to all messages. + Multiple addresses are defined with the ``addXXX()`` methods:: $email = (new Email()) @@ -252,40 +258,6 @@ images inside the HTML contents:: ->html(' ... ...') ; -Global from Address -------------------- - -Instead of calling ``->from()`` *every* time you create a new email, you can -create an event subscriber to set it automatically:: - - // src/EventSubscriber/MailerFromSubscriber.php - namespace App\EventSubscriber; - - use Symfony\Component\EventDispatcher\EventSubscriberInterface; - use Symfony\Component\Mailer\Event\MessageEvent; - use Symfony\Component\Mime\Email; - - class MailerFromSubscriber implements EventSubscriberInterface - { - public function onMessageSend(MessageEvent $event) - { - $message = $event->getMessage(); - - // make sure it's an Email object - if (!$message instanceof Email) { - return; - } - - // always set the from address - $message->from('fabien@example.com'); - } - - public static function getSubscribedEvents() - { - return [MessageEvent::class => 'onMessageSend']; - } - } - .. _mailer-twig: Twig: HTML & CSS