我使用Gmail发送邮件,所以我配置“config.yml”这样的
swiftmailer:
transport: %mailer_transport%
encryption: %mailer_encryption%
auth_mode: %mailer_auth%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
“parameters.yml”这样的
mailer_transport: smtp
mailer_encryption: ssl
mailer_auth: login
mailer_host: smtp.gmail.com
mailer_user: lee@gmail.com
mailer_password: ******
现在我想用更多的邮件帐户发送邮件为不同的目标。 例如:使用lee@gmail.com用于发送邮件的欢迎;使用lee1@gmail.com发送邮件的重置密码。
我应该怎么配置swiftmailer?
如果你正在使用Swiftmailer 2.3.3你有可能让一切简单:
在parameters.yml加:
mailer2_transport: smtp
mailer2_encryption: ssl
mailer2_auth_mode: login
mailer2_host: smtp.gmail.com
mailer2_user: your@gmail.com
mailer2_password: *******
在config.yml进行更改:
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%
在代码中,如果你写:
$mailer = $this->get('swiftmailer.mailer.mailer2');
你会从你的部分设置;
如果你写的:
$mailer = $this->get('swiftmailer.mailer.default');
要么
$mailer = $this->get('mailer'); // default configuration
您将使用默认设置部分;
该SwiftmailerBundle,管理邮件程序的配置,允许您设置只有一个默认配置。 然而,它的相当简单的安装anothers。 只需直接使用Swiftmailer或定义你与其他配置自己的邮件类。
/**
* 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'));
}
您可以定义为许多服务,有不同的配置,只要你想。 对于exemplo,看看下面的例子。
<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>
然后,在你的代码,你会做这样的事情。
$mySecondMailer = new \Swift_Mailer($this->get('mysecond.transport.smtp'));
这应该够了吧。
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
这应该工作: 从文档
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);