4

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:       lee@gmail.com
mailer_password:   ******

Now I want to use more mail accounts to send mails for different goals. eg:Use lee@gmail.com send mails for welcome;Use lee1@gmail.com send mails for reset password.

What should I config swiftmailer?

AnFi
  • 10,493
  • 3
  • 23
  • 47
enflee
  • 51
  • 1
  • 1
  • 3

4 Answers4

22

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: your@gmail.com
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;

aalaap
  • 4,145
  • 5
  • 52
  • 59
Andy
  • 221
  • 2
  • 4
  • Please not that the auth handler is being shared. If you use two mailers with this setup, with different username/passwords this won't work. See https://github.com/symfony/SwiftmailerBundle/pull/70 and https://github.com/symfony/SwiftmailerBundle/issues/73 – rolandow Feb 20 '14 at 10:46
  • 3
    Just want to point out that you have to have the 'default' mailer named 'default' otherwise a dummy 'default' mailer is created for you and it will be used as default instead. I believe this is a bug. – David Lin May 23 '14 at 01:23
  • The auth handler issue identified by rolandow has been fixed in Swiftmailer. – Acyra Mar 13 '16 at 14:17
  • Please how apply this response using symfony 4? – Khalil Apr 16 '19 at 10:08
1

The SwiftmailerBundle, that manages the mailer configuration, allows you to setup only one default configuration. However it's pretty straightforward to setup anothers. Just use the Swiftmailer directly or define you 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 defined as many services, with different configuration, as you want. For exemplo, look 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 would do something like.

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

That should do the trick.

P. R. Ribeiro
  • 2,999
  • 1
  • 19
  • 20
1

This would be how your config file will look like.

app/config/config.yml

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

Now, you can use any of the mailer in following fashion:

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

For more details see: symfony documentry

Community
  • 1
  • 1
Nikhil Pareek
  • 734
  • 9
  • 24
0

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