Skip to content

Commit c9e6249

Browse files
committed
Document the way to configure emails globally
1 parent 07b139b commit c9e6249

File tree

1 file changed

+76
-2
lines changed

1 file changed

+76
-2
lines changed

mailer.rst

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,7 @@ both strings or address objects::
320320
.. tip::
321321

322322
Instead of calling ``->from()`` *every* time you create a new email, you can
323-
create an :doc:`event subscriber </event_dispatcher>` and listen to the
324-
:class:`Symfony\\Component\\Mailer\\Event\\MessageEvent` event to set the
323+
:ref:`configure emails globally <mailer-configure-email-globally>` to set the
325324
same ``From`` email to all messages.
326325

327326
.. note::
@@ -373,6 +372,12 @@ header, etc.) but most of the times you'll set text headers::
373372
// ...
374373
;
375374

375+
.. tip::
376+
377+
Instead of calling ``->addTextHeader()`` *every* time you create a new email, you can
378+
:ref:`configure emails globally <mailer-configure-email-globally>` to set the same
379+
headers to all sent emails.
380+
376381
Message Contents
377382
~~~~~~~~~~~~~~~~
378383

@@ -448,6 +453,75 @@ images inside the HTML contents::
448453
->html('<img src="cid:logo"> ... <img src="cid:footer-signature"> ...')
449454
;
450455

456+
.. _mailer-configure-email-globally:
457+
458+
Configuring Emails Globally
459+
---------------------------
460+
461+
Instead of calling ``->from()`` on each Email you create, you can configure this
462+
value globally so that it is set on all sent emails. The same is true with ``->to()``
463+
and headers.
464+
465+
.. configuration-block::
466+
467+
.. code-block:: yaml
468+
469+
# config/packages/dev/mailer.yaml
470+
framework:
471+
mailer:
472+
envelope:
473+
sender: 'fabien@example.com'
474+
recipients: ['foo@example.com', 'bar@example.com']
475+
headers:
476+
from: 'Fabien <fabien@example.com>'
477+
bcc: 'baz@example.com'
478+
X-Custom-Header: 'foobar'
479+
480+
.. code-block:: xml
481+
482+
<!-- config/packages/mailer.xml -->
483+
<?xml version="1.0" encoding="UTF-8" ?>
484+
<container xmlns="http://symfony.com/schema/dic/services"
485+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
486+
xmlns:framework="http://symfony.com/schema/dic/symfony"
487+
xsi:schemaLocation="http://symfony.com/schema/dic/services
488+
https://symfony.com/schema/dic/services/services-1.0.xsd
489+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
490+
491+
<!-- ... -->
492+
<framework:config>
493+
<framework:mailer>
494+
<framework:envelope>
495+
<framework:sender>fabien@example.com</framework:sender>
496+
<framework:recipients>foo@example.com</framework:recipients>
497+
<framework:recipients>bar@example.com</framework:recipients>
498+
</framework:envelope>
499+
<framework:header name="from">Fabien <fabien@example.com></framework:header>
500+
<framework:header name="bcc">baz@example.com</framework:header>
501+
<framework:header name="X-Custom-Header">foobar</framework:header>
502+
</framework:mailer>
503+
</framework:config>
504+
</container>
505+
506+
.. code-block:: php
507+
508+
// config/packages/mailer.php
509+
$container->loadFromExtension('framework', [
510+
// ...
511+
'mailer' => [
512+
'envelope' => [
513+
'sender' => 'fabien@example.com',
514+
'recipients' => ['foo@example.com', 'bar@example.com'],
515+
],
516+
'headers' => [
517+
'from' => 'Fabien <fabien@example.com>',
518+
'bcc' => 'baz@example.com',
519+
'X-Custom-Header' => 'foobar',
520+
],
521+
],
522+
]);
523+
524+
451525
Handling Sending Failures
452526
-------------------------
453527

0 commit comments

Comments
 (0)