@@ -153,6 +153,12 @@ both strings or address objects::
153
153
// ...
154
154
;
155
155
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
+
156
162
Multiple addresses are defined with the ``addXXX() `` methods::
157
163
158
164
$email = (new Email())
@@ -252,40 +258,6 @@ images inside the HTML contents::
252
258
->html('<img src="cid:logo"> ... <img src="cid:footer-signature"> ...')
253
259
;
254
260
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
-
289
261
.. _mailer-twig :
290
262
291
263
Twig: HTML & CSS
0 commit comments