swiftmailer configuration for more than one accounts in Symfony 2

徘徊边缘 提交于 2019-12-19 21:48:38

问题


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?


回答1:


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;




回答2:


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.




回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/16474822/swiftmailer-configuration-for-more-than-one-accounts-in-symfony-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!