Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swiftmailer configuration for more than one accounts in Symfony 2

I use gmail to send mails ,so I config ‘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:       [email protected]
mailer_password:   ******

Now I want to use more mail accounts to send mails for different goals. eg:Use [email protected] send mails for welcome;Use [email protected] send mails for reset password.

What should I config swiftmailer?

like image 254
enflee Avatar asked Nov 29 '22 16:11

enflee


1 Answers

If you are using Swiftmailer 2.3.3 you have possibility to make everything simply:

in parameters.yml add:

mailer2_transport: smtp
mailer2_encryption: ssl
mailer2_auth_mode: login
mailer2_host: smtp.gmail.com
mailer2_user: [email protected]
mailer2_password: *******

In config.yml make 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 code if you write:

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

you will get settings from your section;

And if you write:

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

or

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

you will use settings from default section;

like image 87
Andy Avatar answered Dec 31 '22 10:12

Andy