diff --git a/mailer.rst b/mailer.rst index 9c9c1451add..89be86382a4 100644 --- a/mailer.rst +++ b/mailer.rst @@ -320,8 +320,7 @@ both strings or address objects:: .. tip:: Instead of calling ``->from()`` *every* time you create a new email, you can - create an :doc:`event subscriber ` and listen to the - :class:`Symfony\\Component\\Mailer\\Event\\MessageEvent` event to set the + :ref:`configure emails globally ` to set the same ``From`` email to all messages. .. note:: @@ -373,6 +372,12 @@ header, etc.) but most of the times you'll set text headers:: // ... ; +.. tip:: + + Instead of calling ``->addTextHeader()`` *every* time you create a new email, you can + :ref:`configure emails globally ` to set the same + headers to all sent emails. + Message Contents ~~~~~~~~~~~~~~~~ @@ -448,6 +453,75 @@ images inside the HTML contents:: ->html(' ... ...') ; +.. _mailer-configure-email-globally: + +Configuring Emails Globally +--------------------------- + +Instead of calling ``->from()`` on each Email you create, you can configure this +value globally so that it is set on all sent emails. The same is true with ``->to()`` +and headers. + +.. configuration-block:: + + .. code-block:: yaml + + # config/packages/dev/mailer.yaml + framework: + mailer: + envelope: + sender: 'fabien@example.com' + recipients: ['foo@example.com', 'bar@example.com'] + headers: + from: 'Fabien ' + bcc: 'baz@example.com' + X-Custom-Header: 'foobar' + + .. code-block:: xml + + + + + + + + + + fabien@example.com + foo@example.com + bar@example.com + + Fabien <fabien@example.com> + baz@example.com + foobar + + + + + .. code-block:: php + + // config/packages/mailer.php + $container->loadFromExtension('framework', [ + // ... + 'mailer' => [ + 'envelope' => [ + 'sender' => 'fabien@example.com', + 'recipients' => ['foo@example.com', 'bar@example.com'], + ], + 'headers' => [ + 'from' => 'Fabien ', + 'bcc' => 'baz@example.com', + 'X-Custom-Header' => 'foobar', + ], + ], + ]); + + Handling Sending Failures -------------------------