Hello, everyone. I have a question about using the Cake Mailer to send to multiple recipients. Ideally, I would like each recipient to receive their own, individually-addressed email. In Cake 2, this was just a matter of looping through the to() and send() methods. The only way I’ve found to do it in Cake 4 is through the controller:
foreach($toArray as $t) {
$this->getMailer('Mailer')->send('newMail', [$fulltitle, $t->email, $t->first_name]);
}
When I attempt the loop within the mailer itself, only the final recipient gets the message:
// Call Mailer in controller. Like above, but without the loop and passing the entire $toArray
$this->getMailer('Mailer')->send('newMail', [$fulltitle, $toArray]);
// newMail method in Mailer
public function newMail($fulltitle, $toArray)
{
foreach ($toArray as $t) {
$this
->setEmailFormat('html')
->setTo($t->email)
->setFrom(['dba@whereiwork.org' => 'DBA'])
->setSubject(sprintf($fulltitle . " is ready for review"))
->setViewVars([
'first_name' => $t->first_name,
'fulltitle' => $fulltitle,
])
->viewBuilder()
->setTemplate('new_mail')
->setLayout('default');
}
}
So, if I have an array of, say, three email addresses, only the last one gets mailed. Is there a way to do this in the mailer itself, or am I stuck doing it in the controller (not my preference)?