How do I use the PHPMailer clearAddresses() function from Kirby?

I’m using the built-in Kirby email driver, with SMTP protocol; to send emails. I’m iterating through a database every 5 minutes and sending entries that are unsent as emails. At a particular interval, there could be multiple discrete emails to send to different recipients, and some recipients are getting multiple duplicates of the same email.

I can see PHPMailer is powering this, and I did a little research to discover that I may need to use the clearAddresses(); PHPmailer method (see /kirby/vendor/phpmailer/phpmailer/src/PHPMailer.php:3489) in order to prevent duplicates when, for example, sending to a mailing list, which seems relevant here as there may be multiple SMTP sends in quick successions.

Question is, how can I call/access these methods from my Kirby config or plugins?

Something like:

$success = kirby()->email([
'from' => '',
'to' => '',
'subject' => 'Welcome!',
'body' => 'We will never reply',

if($success) {

Thanks in advance for your help.

I think $kirby->email() doesn’t support this. I think you would have to use the PHPMailer class directly.

Wondering if this new 3.4.1 beforeSend() option might be helpful here: Haven’t tested it yet.

Thanks, Sonja.
Can you confirm whether this exposes all functions in the PHP Mailer class? It looks like it does, and therefore could do any modifications to PHPMailer required?

'beforeSend' =>function ($mailer) {
    $mailer->SMTPDebug = SMTP::DEBUG_SERVER;

    return $mailer

Yes, you can use all the functions of phpMailer.

But if I got it right; I guess you need it after you send it. Like afterSend or something like that :thinking: Because you want to clear after the sent is successful.

Ideally, but as a workaround I can clear and re-add with $mailer->addAddress($email, $name ?? ‘’) to make use of the available functionality within Kirby.

beforeSend only works just before sending, and you can modify $phpmailer object by running any methods you want in beforeSend option. You can add, remove, or clear recipients before sending. However, the changes you make will not change the Kirby object, only $phpmailer object is updated for that submission.