Home > Software design >  Multiple smtp servers in symfony mailer
Multiple smtp servers in symfony mailer

Time:10-26

I am trying to configure sending emails with Symfony mailer component. It perfectly works with one DNS (which I have in my .env file). But I want to have 2 of them for example. How can I configure it? I haven't found anything sensible in the documentation.

Here is my mailer.YAML

framework:
    mailer:
        dsn: '%env(MAILER_DSN)%'

I would like to have something like:

    framework:
        mailer_default:
            dsn: '%env(MAILER_DSN1)%'
        mailer_second:
            dsn: '%env(MAILER_DSN2)%'

But such an option is not possible, unfortunately.

CodePudding user response:

You can send to multiple transports according to the documentation here

Example, like this:

# config/packages/mailer.yaml
framework:
    mailer:
        transports:
            main: '%env(MAILER_DSN)%'
            alternative: '%env(MAILER_DSN_IMPORTANT)%'

By default the first transport is used. The other transports can be selected by adding an X-Transport header (which Mailer will remove automatically from the final email):

// Send using first transport ("main"):
$mailer->send($email);

// ... or use the transport "alternative":
$email->getHeaders()->addTextHeader('X-Transport', 'alternative');
$mailer->send($email);
  • Related