4
votes

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?

4

4 Answers

21
votes

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;

1
votes

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.

1
votes

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

0
votes

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('[email protected]' => 'John Doe'))
  ->setTo(array('[email protected]', '[email protected]' => 'A name'))
  ->setBody('Here is the message itself')
  ;

// Send the message
$result = $mailer->send($message);