I am using the ExtendedMailMessage class to try and send an email with an attachments to my clients. The issue is I am sending the email inside a foreach loop and for some reason even though I am defining a new attachment the new attachment is just being appended to an array and each client gets multiple attachments instead of one.
$extendedMailer = $this->ci->extendedMailer;
foreach ($emails as $email) {
$attachment = new MailAttachment(
base64_decode($base64String), "report.pdf"
);
try {
$message = new ExtendedTwigMailMessage($this->ci->view, 'mail/pdf-reports.html.twig');
$message->from($config['address_book.admin'])
->addEmailRecipient(new EmailRecipient($email, 'Client'))
->setFromEmail($config['address_book.admin'])
->setReplyEmail($config['address_book.admin'])
->addAttachment($attachment);
$extendedMailer->sendDistinct($message);
}
catch(\Exception $ex) {
var_dump($email);
}
}
The first client will receive 1 attachment and then the 2nd will receive 2 attachments, 3rd will receive 3 attachments etc...
How do I just send 1 attachement with each email instead of appending it to the old attachments