Search code examples
phpsymfonyswiftmailer

How to set multiple mails in symfony 4 using swiftmailer-bundle


I'm using swiftmailer-bundle for sending emails from my applications

I have added this in env.

MAILER_URL=gmail://mailExample@mail.com:mypassword@localhost?encryption=tls&auth_mode=oauth

And this when i need to sent the email from controller

$message = (new \Swift_Message($objet))
                ->setFrom('mailExample@mail.com','example')
                ->setTo(exemple2@mail.com)
                ->setBody("test")
                )

My question is how to add another mail?, i need to use more than one mail

Can i add two lines of MAILER_URL in env. ??


Solution

  • Check out the official documentation on using multiple mailers.

    https://symfony.com/doc/current/reference/configuration/swiftmailer.html#using-multiple-mailers

    swiftmailer:
        default_mailer: first_mailer
        mailers:
            first_mailer:
                url: '%env(MAILER_URL)%'
            second_mailer:
                url: '%env(SECOND_MAILER_URL)%'
    
    // returns the first mailer
    $container->get('swiftmailer.mailer.first_mailer');
    
    // returns the second mailer
    $container->get('swiftmailer.mailer.second_mailer');