我需要在一个循环中使用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);
}
由于我是在循环内执行的,因此很难将mails数组传递给swift mailer。我正在Console Command内部运行此命令,并且在控制台中收到此错误。
17:14:09错误[app]刷新电子邮件时发生异常 队列:预期的响应代码354,但得到的代码为“ 550”,并带有消息 “ 550 5.7.0请求的操作未执行:每秒电子邮件过多
答案 0 :(得分:0)
我看到电子邮件内容是相同的。那么,为什么不仅使用Cci或Cc代替循环? 这也是性能上的优势
答案 1 :(得分:0)
假设您有一个名为 sendEmail 的函数,用于向在您系统中拥有帐户的所有用户发送电子邮件。
以下说明可以帮助您通过代码实现解决电子邮件错误:
对我来说它有效......
public function sendEmail(\Swift_Mailer $mailer): Response
{
$entityManager=$this->getDoctrine()->getManager();
$connection= $entityManager->getConnection();
$decl12="SELECT id
FROM
user";
$statement97=$connection->prepare($decl12);
$statement97->execute();
$tap=$statement97->fetchAll();
try{
foreach($tap as $i=>$data)
{
//check what type of data taken from variable data by
//var_dump($data);
$test =$this->getDoctrine()->getRepository(User::class)->findOneBy(['id'=>$data]);
$email=$test->getEmail();
$message = (new \Swift_Message('Email Subject'))
->setFrom('admin@gmail.com')
->setTo($email)
->setBody(
"click here to get bonus",
'text/html'
);
$mailer->send($message);
}
}
catch (\Swift_TransportException $e)
{
return new Response($e->getMessage());
}
}