Search code examples
emailphpmailermodx-revolution

How to send multiple emails in MODX in single SMTP session?


I have a script that sends multiple emails in a loop. Between every email, I reset modMail:

$modx->mail->reset()

Sending becomes really slow since modMail needs to connect to the SMTP server again. I would like to send all emails in a single SMTP session.

I found that PHPMailer has a method clearAllRecipients but the modMail class does not have it. So the following command does not work:

$modx->mail->clearAllRecipients()

Is it safe to call PHPMailer's method directly with the following command:

$modx->mail->mailer->clearAllRecipients()

I am a little worried that something will get later broken if the PHPMailer changes. Or what would be a better solution?


Solution

  • PHPMailer's API isn't likely to change in any way like that precisely because it would cause BC breaks. Doing what you suggest should work just fine, though remember you'll need to also set SMTPKeepAlive = true to make it do this. See the reference implementation in the PHPMailer mailing list example.

    If you think that this is something that other modx users would benefit from, you could add it to the wrapper they provide (and submit a PR for it) so that you can be abstracted away from PHPMailer's internal API, and thus protected from future changes that way.