代码之家  ›  专栏  ›  技术社区  ›  vimuth

Symfony SwiftMailer在环路内发送多封邮件

  •  0
  • vimuth  · 技术社区  · 6 年前

    我需要在一个循环内用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('admin@admin.com')
                    ->setTo($mail)
                    ->setBody(
                    $this->templating->render(
                            $template, [
                                'ad' => $ad
                            ]
                    ), 'text/html'
            );
    
            $this->mailer->send($message);
        }
    

    因为我是在循环内进行的,所以很难将邮件数组传递给swift mailer。我在里面查这个 Console Command 我在控制台上看到这个错误。

    17:14:09刷新电子邮件时出现错误[应用程序]异常 队列:预期响应代码354,但得到代码“550”,并显示消息 “550 5.7.0请求的操作未执行:每秒电子邮件太多

    0 回复  |  直到 6 年前
        1
  •  0
  •   JessGabriel    6 年前

    我看到电子邮件的内容是一样的。那么,为什么不只用cci或cc来代替loop呢? 这也是性能上的优势