I have a mailer in my app that’s currently called in a model towards the end of the controller’s save event:
// controller call
$this->Msrs->sendSignMail($recipients, $msr, $grandTotal, $user->full_name, $module);
// model function that calls the mailer
public function sendSignMail($recipients, $msr, $grandTotal, $fullName, $module)
{
foreach ($recipients as $r) {
$this->getMailer('Msr')->send('signMsr', [$msr, $grandTotal, $r->email, $r->first_name, $fullName, $module]);
}
}
Here is the signMsr()
method in MsrMailer
:
public function signMsr($msr, $grandTotal, $email, $firstName, $requester, $module)
{
$this
->setEmailFormat('html')
->setTo($email)
->setFrom([// 'from' email address])
->setSubject(sprintf($subjStart . $subject))
->setViewVars([
// ViewVars here
])
->viewBuilder()
->setTemplate('sign_msr')
->setLayout('default');
}
This all works fine as long as the recipient list is short (say, 1-3 people); however, if there are many recipients, the save process bogs down, and SMTP will sometimes time out before all of the emails are sent.
Because of our corporate email setup, I have to send the messages securely, over port 465, so I know that generates additional overhead. I feel like this could be solved using an afterSave event handler, but I’m stumped as to how to go about it. Here’s what I’ve got in my controller:
$mailer = new MsrMailer();
$this->Msrs->getEventManager()->on($this->getMailer('Msr'));
And here’s what I’ve got so far in MsrMailer
:
public function implementedEvents() : array
{
return [
'Model.afterSave' => 'sendTheMail'
];
}
public function sendTheMail(EventInterface $event, EntityInterface $entity, ArrayObject $options)
{
// What do I do here????
}
This is skeletal, obviously. I don’t know what the next step is in terms of fleshing out sendTheMail
so that signMsr
gets executed for every recipient and the mail gets sent. I’m not even sure it’s being called, because I can’t debug $event, $entity, or $options. Nothing happens after the save is complete. I can confirm, however, that implementedEvents()
is firing, or at least being recognized.
What do I do next to get this working?