1

我想使用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);
                }
4

1 回答 1

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();
于 2014-09-19T12:06:47.183 回答