Multiple Swiftmailer instances in Symfony2

Symfony2 uses Swiftmailer to send emails.

To use and configure Swiftmailer in Symfony2, you must use the same configuration as in documents, for example. using YAML:

swiftmailer: transport: smtp encryption: ssl auth_mode: login host: smtp.gmail.com username: your_username password: your_password 

Swiftmailer is defined in Symfony2 as a service, and an instance of it can be obtained in the controller as follows:

 $mailerinstance = $this->get('mailer'); 

Now suppose that Swiftmailer requires two different configurations, for example. one that uses email buffering (for example, for a scheduled newsletter), and another that immediately sends all new emails (for example, for a lost password). Thus, I assume that two separate Swiftmailer instances should be defined. How can I do this in symfony2?

+6
source share
2 answers

There is no default symfony method for two different instances. But you can simply create a new class that extends the swiftmailer, make it a service, and simply pass your other configuration to the parent constructor.

+7
source
 swiftmailer: default_mailer: second_mailer mailers: first_mailer: # ... second_mailer: # ... // ... // returns the first mailer $container->get('swiftmailer.mailer.first_mailer'); // also returns the second mailer since it is the default mailer $container->get('swiftmailer.mailer'); // returns the second mailer $container->get('swiftmailer.mailer.second_mailer'); 

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

+4
source

All Articles