aalaap aalaap - 4 months ago 55
YAML Question

How do I inject different SwiftMailer mailers into different services in Symfony?

I have a Symfony 2.8 project that requires usage of two separate mailing services (eg. Google SMTP and Yahoo SMTP). I have configured them according to the docs:

swiftmailer:
default_mailer: mailer_google
mailers:
mailer_google:
transport: smtp
host: %mailer_google_host%
port: %mailer_google_port%
encryption: tls
username: %mailer_google_username%
password: %mailer_google_password%
mailer_yahoo:
transport: smtp
host: %mailer_yahoo_host%
port: %mailer_yahoo_port%
encryption: tls
username: %mailer_yahoo_username%
password: %mailer_yahoo_password%


The values for the
%variables%
are all defined in
parameters.yml
, which are not relevant here.


I'm trying to use a custom Mailer class which is used to define two different services, like this:

aalaap.services.mailer_google:
class: Aalaap\AppBundle\Services\Mail\Mailer
arguments:
- @mailer
- '%mailer_google_sender%'

aalaap.services.mailer_yahoo:
class: Aalaap\AppBundle\Services\Mail\Mailer
arguments:
- @mailer_yahoo
- '%mailer_yahoo_sender%'


Now, because the default mailer is set to
mailer_google
, in the first service definition, only specifying
@mailer
works fine. However, the second service can't find
@mailer_yahoo
.


ServiceNotFoundException in CheckExceptionOnInvalidReferenceBehaviorPass.php line 58:
The service "aalaap.services.mailer_yahoo" has a dependency on a non-existent service "mailer_yahoo".


I've tried it with
@mailer.mailer_yahoo
, but that doesn't work either. If I change the first service definition to use the specific
@mailer_google
instead of just the default
@mailer
, that doesn't work either. No luck with
@swiftmailer.mailers.mailer_yahoo
.

How do I inject the second (or non-default) mailer into a service?

Answer

So, this is how to setup multiple mailers accounts and handle injection with Symfony :

Setup the swiftmailer config :

swiftmailer:
    default_mailer: mailer_google
    mailers:
        mailer_google:
            transport: smtp
            host: %mailer_google_host%
            port: %mailer_google_port%
            encryption: tls
            username: %mailer_google_username%
            password: %mailer_google_password%
        mailer_yahoo:
            transport: smtp
            host: %mailer_yahoo_host%
            port: %mailer_yahoo_port%
            encryption: tls
            username: %mailer_yahoo_username%
            password: %mailer_yahoo_password%

Then inject the wanted mailer (for example, google mailer) in services this way :

aalaap.services.mailer_google:
    class: Aalaap\AppBundle\Services\Mail\Mailer
    arguments:
      - @swiftmailer.mailer.mailer_google
      - '%mailer_google_sender%'

Or (because in that case google mailer is the default mailer) :

aalaap.services.mailer_google:
    class: Aalaap\AppBundle\Services\Mail\Mailer
    arguments:
      - @swiftmailer.mailer.default_mailer
      - '%mailer_google_sender%'