Skip to content

Better explain how to inject mailers different than the default one #9410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion reference/configuration/swiftmailer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ key (the default mailer is identified by the ``default_mailer`` option):
),
));

Each mailer is registered as a service::
Each mailer is registered automatically as a service with these IDs::

// ...

Expand All @@ -362,3 +362,70 @@ Each mailer is registered as a service::
When configuring multiple mailers, options must be placed under the
appropriate mailer key of the configuration instead of directly under the
``swiftmailer`` key.

When using :ref:`autowiring <services-autowire>` only the default mailer is
injected when type-hinting some argument with the ``\Swift_Mailer`` class. If
you need to inject a different mailer in some service, use any of these
alternatives based on the :ref:`service binding <services-binding>` feature:

.. configuration-block::

.. code-block:: yaml

# config/services.yaml
services:
_defaults:
bind:
# this injects the second mailer when type-hinting constructor arguments with \Swift_Mailer
\Swift_Mailer: '@swiftmailer.mailer.second_mailer'
# this injects the second mailer when a service constructor argument is called $specialMailer
$specialMailer: '@swiftmailer.mailer.second_mailer'

App\Some\Service:
# this injects the second mailer only for this argument of this service
$differentMailer: '@swiftmailer.mailer.second_mailer'

# ...

.. code-block:: xml

<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<defaults autowire="true" autoconfigure="true" public="false">
<!-- this injects the second mailer when type-hinting constructor arguments with \Swift_Mailer -->
<bind key="\Swift_Mailer">@swiftmailer.mailer.second_mailer</bind>
<!-- this injects the second mailer when a service constructor argument is called $specialMailer -->
<bind key="$specialMailer">@swiftmailer.mailer.second_mailer</bind>
</defaults>

<service id="App\Some\Service">
<!-- this injects the second mailer only for this argument of this service -->
<argument key="$differentMailer">@swiftmailer.mailer.second_mailer</argument>
</service>

<!-- ... -->
</services>
</container>

.. code-block:: php

// config/services.php
use App\Some\Service;
use Symfony\Component\DependencyInjection\Reference;
use Psr\Log\LoggerInterface;

$container->register(Service::class)
->setPublic(true)
->setBindings(array(
// this injects the second mailer when this service type-hints constructor arguments with \Swift_Mailer
\Swift_Mailer => '@swiftmailer.mailer.second_mailer',
// this injects the second mailer when this service has a constructor argument called $specialMailer
'$specialMailer' => '@swiftmailer.mailer.second_mailer',
))
;