Symfony swiftmailer在循环内发送多封邮件

问题描述 投票:0回答:1

我需要在循环中使用symfony swiftmailer发送多封邮件。这是我的代码。

public function __construct($name = null, \Swift_Mailer $mailer, EngineInterface $templating) {
        parent::__construct($name)
        $this->mailer = $mailer;
        $this->templating = $templating;
}

protected function execute(InputInterface $input, OutputInterface $output) {
  foreach ($ads as $ad) {
                if($counter == 10) break;
                $this->sendMail($ad->getUser()->getMail(), $ad, 'matching');
                $counter++;
  }
}

protected function sendMail($mail, $ad, $template = '') {

        if($template == 'matching'){
            $template = 'emails/matching-cars.html.twig';
        }elseif($template == 'owner'){
            $template = 'emails/matching-car-owners.html.twig';
        }

        $message = (new \Swift_Message('Hello Email'))
                ->setFrom('[email protected]')
                ->setTo($mail)
                ->setBody(
                $this->templating->render(
                        $template, [
                            'ad' => $ad
                        ]
                ), 'text/html'
        );

        $this->mailer->send($message);
    }

由于我在循环中执行它很难将邮件数组传递给swift邮件程序。我在Console Command中运行这个,我在控制台中收到此错误。

17:14:09错误[app]在刷新电子邮件队列时发生异常:预期响应代码354但得到代码“550”,消息“550 5.7.0未执行请求的操作:每秒发送的电子邮件太多

symfony swiftmailer
1个回答
0
投票

我看到电子邮件的内容是相同的。那么,为什么不只使用Cci或Cc而不是循环?这也是性能的优势

© www.soinside.com 2019 - 2024. All rights reserved.