doupike2351 2017-09-23 07:14
浏览 107

Yii2 SwiftMailer发送邮件两次

I write a game for football fans. So, I have to send similar mails to a group of people (not completely duplicated e-mail copies). When I send the mails in a cycle - Yii framework sends the mails twice. I suppose - it is because of the static variable Yii::$app. Can someone give me a hint, please. A code for example.

foreach ($oRace->user as $currUser) {
        $htmlContent = $this->renderPartial('start_race', ['oRace' => $oRace]);
        Yii::$app->mailer->compose()
                ->setFrom('info@example.com')
                ->setTo($currUser->mail)
                ->setSubject('Race "' . $raceName . '" has Started')
                ->setHtmlBody($htmlContent)
                ->send();
    }

Thanks all in advance!

My Mailer config.

    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'useFileTransport' => false,
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'mail.example.eu',
            'username' => 'support@example.com',
            'password' => 'password',
            'port' => '587',
            'encryption' => 'TLS',
        ]
    ],

One more thing. The last mail in the cycle is never duplicated (only the last).

Another failed option. Yii::$app->mailer->sendMultiple($allMails);

  • 写回答

3条回答 默认 最新

  • dougui4325 2018-01-30 11:21
    关注

    From the provided code snippets, there are 3 possible reasons for that. Either:

    • $oRace->user contains every user twice
    • $currUser->mail contains the email twice like `email@example.com;email@example.com"
    • something is wrong inside the send function of SwiftMailer
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效