Configuring swiftmailer for multiple accounts in Symfony 2

I use gmail to send emails, so I configure config.yml like this

swiftmailer: transport: %mailer_transport% encryption: %mailer_encryption% auth_mode: %mailer_auth% host: %mailer_host% username: %mailer_user% password: %mailer_password% 

'parameters.yml, like this

 mailer_transport: smtp mailer_encryption: ssl mailer_auth: login mailer_host: smtp.gmail.com mailer_user: lee@gmail.com mailer_password: ****** 

Now I want to use more email accounts to send emails for different purposes. for example: use lee@gmail.com to send greetings; use lee1@gmail.com to send reset password emails.

What do I need to configure swiftmailer?

+4
source share
4 answers

If you use Swiftmailer 2.3.3, you have the opportunity to make everything simple:

in .yml add options:

 mailer2_transport: smtp mailer2_encryption: ssl mailer2_auth_mode: login mailer2_host: smtp.gmail.com mailer2_user: your@gmail.com mailer2_password: ******* 

In config.yml, make the changes:

 swiftmailer: default_mailer: mailer mailers: mailer: transport: %mailer_transport% host: %mailer_host% username: %mailer_user% password: %mailer_password% encryption: %mailer_encryption% auth_mode: %mailer_auth_mode% mailer2: transport: %mailer2_transport% host: %mailer2_host% username: %mailer2_user% password: %mailer2_password% encryption: %mailer2_encryption% auth_mode: %mailer2_auth_mode% 

In the code, if you write:

 $mailer = $this->get('swiftmailer.mailer.mailer2'); 

You will get the settings from your section;

And if you write:

 $mailer = $this->get('swiftmailer.mailer.default'); 

or

 $mailer = $this->get('mailer'); // default configuration 

You will use the settings from the default section;

+20
source

The SwiftmailerBundle, which controls the configuration of the mail program, allows you to configure only one default configuration. However, it’s quite simple to set up strangers. Just use Swiftmailer directly or define your own mailer classes with other configurations.

 /** * Gets the 'mailer' service. * * This service is shared. * This method always returns the same instance of the service. * * @return Swift_Mailer A Swift_Mailer instance. */ protected function getMailerService() { return $this->services['mailer'] = new \Swift_Mailer($this->get('swiftmailer.transport')); } 

You can define as many services as you want with a different configuration. For example, see the following example.

 <service id="mysecond.transport.smtp" class="%swiftmailer.transport.smtp.class%" public="false"> <argument type="service" id="swiftmailer.transport.buffer" /> <argument type="collection"> <argument type="service" id="swiftmailer.transport.authhandler" /> </argument> <argument type="service" id="swiftmailer.transport.eventdispatcher" /> <call method="setHost"><argument>%mysecond.transport.smtp.host%</argument></call> <call method="setPort"><argument>%mysecond.transport.smtp.port%</argument></call> <call method="setEncryption"><argument>%mysecond.transport.smtp.encryption%</argument></call> <call method="setUsername"><argument>%mysecond.transport.smtp.username%</argument></call> <call method="setPassword"><argument>%mysecond.transport.smtp.password%</argument></call> <call method="setAuthMode"><argument>%mysecond.transport.smtp.auth_mode%</argument></call> <call method="setTimeout"><argument>%mysecond.transport.smtp.timeout%</argument></call> <call method="setSourceIp"><argument>%mysecond.transport.smtp.source_ip%</argument></call> </service> 

Then in your code you will do something like.

 $mySecondMailer = new \Swift_Mailer($this->get('mysecond.transport.smtp')); 

That should do the trick.

+1
source

It will look like your configuration file.

application /Config/config.yml

  swiftmailer: default_mailer: second_mailer mailers: first_mailer: # ... second_mailer: # ... 

Now you can use any email program as follows:

 // 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'); 

See symfony documentation for more details.

+1
source

this should work: from the documentation

 require_once 'lib/swift_required.php'; // Create the Transport $transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25) ->setUsername('your username') ->setPassword('your password') ; /* You could alternatively use a different transport such as Sendmail or Mail: // Sendmail $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs'); // Mail $transport = Swift_MailTransport::newInstance(); */ // Create the Mailer using your created Transport $mailer = Swift_Mailer::newInstance($transport); // Create a message $message = Swift_Message::newInstance('Wonderful Subject') ->setFrom(array(' john@doe.com ' => 'John Doe')) ->setTo(array(' receiver@domain.org ', ' other@domain.org ' => 'A name')) ->setBody('Here is the message itself') ; // Send the message $result = $mailer->send($message); 
0
source

All Articles