Skip to content

Commit 8070575

Browse files
committed
minor #12351 Simplified the mailer docs about a global from address (javiereguiluz)
This PR was merged into the 4.3 branch. Discussion ---------- Simplified the mailer docs about a global from address I'd like to remove this example because this is not about a Mailer feature but about other Symfony features that are explained in other articles. Commits ------- eab240e Simplified the mailer docs about a global from address
2 parents 7d6276d + eab240e commit 8070575

File tree

1 file changed

+6
-34
lines changed

1 file changed

+6
-34
lines changed

mailer.rst

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ both strings or address objects::
153153
// ...
154154
;
155155

156+
.. tip::
157+
158+
Instead of calling ``->from()`` *every* time you create a new email, you can
159+
create an :doc:`event subscriber </event_dispatcher>` and listen to the
160+
``MessageEvent::class`` event to set the same ``From`` email to all messages.
161+
156162
Multiple addresses are defined with the ``addXXX()`` methods::
157163

158164
$email = (new Email())
@@ -252,40 +258,6 @@ images inside the HTML contents::
252258
->html('<img src="cid:logo"> ... <img src="cid:footer-signature"> ...')
253259
;
254260

255-
Global from Address
256-
-------------------
257-
258-
Instead of calling ``->from()`` *every* time you create a new email, you can
259-
create an event subscriber to set it automatically::
260-
261-
// src/EventSubscriber/MailerFromSubscriber.php
262-
namespace App\EventSubscriber;
263-
264-
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
265-
use Symfony\Component\Mailer\Event\MessageEvent;
266-
use Symfony\Component\Mime\Email;
267-
268-
class MailerFromSubscriber implements EventSubscriberInterface
269-
{
270-
public function onMessageSend(MessageEvent $event)
271-
{
272-
$message = $event->getMessage();
273-
274-
// make sure it's an Email object
275-
if (!$message instanceof Email) {
276-
return;
277-
}
278-
279-
// always set the from address
280-
$message->from('fabien@example.com');
281-
}
282-
283-
public static function getSubscribedEvents()
284-
{
285-
return [MessageEvent::class => 'onMessageSend'];
286-
}
287-
}
288-
289261
.. _mailer-twig:
290262

291263
Twig: HTML & CSS

0 commit comments

Comments
 (0)