@@ -151,9 +151,11 @@ both strings or address objects::
151
151
// ...
152
152
;
153
153
154
- .. versionadded :: 4.4
154
+ .. tip ::
155
155
156
- The ``Address::fromString() `` method was introduced in Symfony 4.4.
156
+ Instead of calling ``->from() `` *every * time you create a new email, you can
157
+ create an :doc: `event subscriber </event_dispatcher >` and listen to the
158
+ ``MessageEvent::class `` event to set the same ``From `` email to all messages.
157
159
158
160
Multiple addresses are defined with the ``addXXX() `` methods::
159
161
@@ -254,40 +256,6 @@ images inside the HTML contents::
254
256
->html('<img src="cid:logo"> ... <img src="cid:footer-signature"> ...')
255
257
;
256
258
257
- Global from Address
258
- -------------------
259
-
260
- Instead of calling ``->from() `` *every * time you create a new email, you can
261
- create an event subscriber to set it automatically::
262
-
263
- // src/EventSubscriber/MailerFromSubscriber.php
264
- namespace App\EventSubscriber;
265
-
266
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
267
- use Symfony\Component\Mailer\Event\MessageEvent;
268
- use Symfony\Component\Mime\Email;
269
-
270
- class MailerFromSubscriber implements EventSubscriberInterface
271
- {
272
- public function onMessageSend(MessageEvent $event)
273
- {
274
- $message = $event->getMessage();
275
-
276
- // make sure it's an Email object
277
- if (!$message instanceof Email) {
278
- return;
279
- }
280
-
281
- // always set the from address
282
- $message->from('fabien@example.com');
283
- }
284
-
285
- public static function getSubscribedEvents()
286
- {
287
- return [MessageEvent::class => 'onMessageSend'];
288
- }
289
- }
290
-
291
259
.. _mailer-twig :
292
260
293
261
Twig: HTML & CSS
0 commit comments