带装饰器的Swift Mailer不在Yii2发送邮件

时间:2014-05-08 13:36:47

标签: php yii2

我想使用decorator向用户发送自定义消息。

出于某种原因,相同的消息会被发送。

为什么?

$replacements = array();
                $replacements['f1@d.net'] = array(
                    '{v1}' => 'valoare1',
                    '{v2}' => 'valoare2',
                );
                $replacements['f2@d.net'] = array(
                    '{v1}' => 'valoare21',
                    '{v2}' => 'valoare22',
                );
                $replacements['f3@d.net'] = array(
                    '{v1}' => 'valoare31',
                    '{v2}' => 'valoare32',
                );
                $replacements['f4@d.net'] = array(
                    '{v1}' => 'valoare41',
                    '{v2}' => 'valoare42',
                );
                $replacements['f5@d.net'] = array(
                    '{v1}' => 'valoare51',
                    '{v2}' => 'valoare52',
                );

                echo count($replacements);

                $decorator = new \Swift_Plugins_DecoratorPlugin($replacements);

                $mailer = \Swift_Mailer::newInstance(
                                \Swift_SmtpTransport::newInstance('smtp', 25)
                );

                $mailer->registerPlugin($decorator);

                $message = \Swift_Message::newInstance()->setSubject('title {v1}')->setBody('layout {v2}');

                foreach ($replacements as $email => $replacement) {
                    $message->setFrom(array($email => 'to me'));
//                    $message->setTo($email);
                    $message->addTo($email);
                    $mailer->send($message);
                }

1 个答案:

答案 0 :(得分:0)

在confi / main.php文件中的组件下添加以下内容。

'mail' => [
  'class' => 'yii\swiftmailer\Mailer',
  'useFileTransport'=>false,
  'transport' => [
    'class' => 'Swift_SmtpTransport',
    'host' => 'host_name',
    'username' => 'user_name',
    'password' => 'password',
    'port' => '587',
    'encryption' => 'tls'
  ],
]

使用

发送邮件
\Yii::$app->mailer->compose()
  ->setHtmlBody("mail_content")
  ->setFrom('from_email_id')
  ->setTo('to_email_id')
  ->setSubject("Subject")
  ->send();