我想从不同的电子邮件帐户向用户发送电子邮件
如何配置多个$mailer组件
以下是我目前在main-local.php
中实现的内容
'mailer' => [
'class' => 'yii'swiftmailer'Mailer',
'useFileTransport'=>false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'xxxxxxxxx',
'password' => 'yyyyyyyyyy',
'port' => '465',
'encryption' => 'ssl',
]
],
你试过这个吗?
'mailer' => [
'class' => 'yii'swiftmailer'Mailer',
'useFileTransport'=>false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'xxxxxxxxx',
'password' => 'yyyyyyyyyy',
'port' => '465',
'encryption' => 'ssl',
]
],
'mailerb' => [
'class' => 'yii'swiftmailer'Mailer',
'useFileTransport'=>false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'xxxxxxxxx',
'password' => 'yyyyyyyyyy',
'port' => '465',
'encryption' => 'ssl',
]
],
访问:
Yii::$app->mailer->compose()
Yii::$app->mailerb->compose()
要使用Mailer,您应该像在main-local.php 中那样在应用程序配置中配置它
要发送电子邮件,您可以使用以下代码:
Yii::$app->mailer->compose('contact/html', ['contactForm' => $form])
->setFrom('from@domain.com')
->setTo($form->email)
->setSubject($form->subject)
->send();
其中:
->setFrom(array('mail1@gmail.com' => 'NAME','mail2@gmail.com' => 'NAME2'))
如果此消息来自多个人,则可以传递一组地址。除电子邮件地址外,您还可以使用以下格式指定发件人姓名:[电子邮件=>姓名]。
Gmail不允许覆盖FROM名称,除非你向Gmail证明了你拥有的经过验证的电子邮件地址。选择其他电子邮件服务器,或者转到你的gmail设置,将其更改为另一个可以接收电子邮件的有效电子邮件地址。