diff --git a/reference/configuration/swiftmailer.rst b/reference/configuration/swiftmailer.rst index 6dc96856e18..5eedc920987 100644 --- a/reference/configuration/swiftmailer.rst +++ b/reference/configuration/swiftmailer.rst @@ -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:: // ... @@ -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 ` 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 ` 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 + + + + + + + + + @swiftmailer.mailer.second_mailer + + @swiftmailer.mailer.second_mailer + + + + + @swiftmailer.mailer.second_mailer + + + + + + + .. 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', + )) + ;