Skip to content

Commit 72140c4

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

File tree

1 file changed

+87
-2
lines changed

1 file changed

+87
-2
lines changed

mailer.rst

Lines changed: 87 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,86 @@ 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+
# config/packages/mailer.yaml
466+
framework:
467+
mailer:
468+
envelope:
469+
sender: 'fabien@example.com'
470+
recipients: ['foo@example.com', 'bar@example.com']
471+
headers:
472+
from: 'Fabien <fabien@example.com>'
473+
bcc: 'baz@example.com'
474+
X-Custom-Header: 'foobar'
475+
476+
.. configuration-block::
477+
478+
.. code-block:: yaml
479+
480+
# config/packages/dev/mailer.yaml
481+
framework:
482+
mailer:
483+
envelope:
484+
sender: 'fabien@example.com'
485+
recipients: ['foo@example.com', 'bar@example.com']
486+
headers:
487+
from: 'Fabien <fabien@example.com>'
488+
bcc: 'baz@example.com'
489+
X-Custom-Header: 'foobar'
490+
491+
.. code-block:: xml
492+
493+
<!-- config/packages/mailer.xml -->
494+
<?xml version="1.0" encoding="UTF-8" ?>
495+
<container xmlns="http://symfony.com/schema/dic/services"
496+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
497+
xmlns:framework="http://symfony.com/schema/dic/symfony"
498+
xsi:schemaLocation="http://symfony.com/schema/dic/services
499+
https://symfony.com/schema/dic/services/services-1.0.xsd
500+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
501+
502+
<!-- ... -->
503+
<framework:config>
504+
<framework:mailer>
505+
<framework:envelope>
506+
<framework:sender>fabien@example.com</framework:sender>
507+
<framework:recipients>foo@example.com</framework:recipients>
508+
<framework:recipients>bar@example.com</framework:recipients>
509+
</framework:envelope>
510+
<framework:header name="from">Fabien <fabien@example.com></framework:header>
511+
<framework:header name="bcc">baz@example.com</framework:header>
512+
<framework:header name="X-Custom-Header">foobar</framework:header>
513+
</framework:mailer>
514+
</framework:config>
515+
</container>
516+
517+
.. code-block:: php
518+
519+
// config/packages/mailer.php
520+
$container->loadFromExtension('framework', [
521+
// ...
522+
'mailer' => [
523+
'envelope' => [
524+
'sender' => 'fabien@example.com',
525+
'recipients' => ['foo@example.com', 'bar@example.com'],
526+
],
527+
'headers' => [
528+
'from' => 'Fabien <fabien@example.com>',
529+
'bcc' => 'baz@example.com',
530+
'X-Custom-Header' => 'foobar',
531+
],
532+
],
533+
]);
534+
535+
451536
Handling Sending Failures
452537
-------------------------
453538

0 commit comments

Comments
 (0)