【问题标题】:Yii2 configure multiple mail components using swiftmailerYii2 使用 swiftmailer 配置多个邮件组件
【发布时间】:2016-05-13 14:11:55
【问题描述】:

我想将来自不同电子邮件帐户的电子邮件发送给用户。
如何配置多个 $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',
    ]
],

【问题讨论】:

  • 为什么不像您的示例所示那样定义另一个 Swift 邮件程序(使用不同的名称)?您需要支持多少个电子邮件帐户?
  • 我试过了,但它不起作用
  • 你做了什么?对第一条评论的内容进行编辑。您是否创建了另一个具有不同名称的邮件程序组件并在创建邮件程序对象时使用了该名称?

标签: php yii2 swiftmailer


【解决方案1】:

你试过了吗?

'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()

【讨论】:

  • 是的,它的工作原理,只需要为第二个添加视图路径它不会获得默认视图路径。
  • 'viewPath' => DIR 。 "/../../common/mail",
  • 在该邮件程序中新建邮件程序对象
【解决方案2】:

要使用 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'))

如果此消息来自多人,您可以传递一个地址数组。除了电子邮件地址之外,您还可以使用以下格式指定发件人姓名:[email => name]。

Gmail 不允许覆盖 FROM 名称,但您向 gmail 证明您拥有的经过验证的电子邮件地址除外。选择不同的电子邮件服务器或转到您的 gmail 设置并将其更改为另一个您可以接收电子邮件的有效电子邮件地址。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多