0

Ok, so I need to fetch all email addresses from a database and send an email notice to each in a batch. Using addAddress() would reveal all destination emails to every recipient. Using addBCC() fixes it but now there is another problem which is a missing "To:" header and I'm not sure how to add it.

A quick and dirty workaround would be something like this:

while($email = mysql_fetch_row($res)[0] {
   $mail->addAddress($email);
   $mail->send();
   $mail->clearAllRecipients();
}

This is very straightforward and addBCC() is not necessary here at all. Except it has to send as many times as there are email addresses. Obviously, not very elegant and much slower at that. I assume one would still have to stick with addBCC() supplemented with something like addCustomHeader(), but I fail to see how this combination wouldn't meet the same fate as addAddress() with all the addresses added before send(). Does a true workaround exist at all?

Puddintane
  • 5
  • 1
  • 3
  • Even if you get SPF, DKIM and DMARC set up properly, Bcc mail has a high chance of being delivered to spam folders. I routinely delete automatically anything that doesn't have my name in the To: or Cc: fields. You might find that sending individual emails is the only reliable thing to do. – Tangentially Perpendicular Jan 26 '22 at 08:16
  • That's not really the purpose of BCC. Also your email might get flagged as spam if it has too many recipients – m_j_alkarkhi Jan 26 '22 at 08:19
  • Are you saying even custom header wouldn't prevent it from going to spam? – Puddintane Jan 26 '22 at 08:31

1 Answers1

0

You can just use addBCC by itself. If you don't add a to address, PHPMailer will create a placeholder called undisclosed-recipients:;, which is an empty group address, so sends to nobody. This is a very common pattern that's been used for decades. Different services will have different limits for the number of BCC addresses you can use for a single message, so you'll need to check their docs.

Generally speaking though, you are better off sending each message individually. The fastest and most effective way to send is to run a local mail server and use it as a relay. This will accept messages as fast as you can generate them, and will automatically deal with queueing, retries and bounces. The PHPMailer wiki has an article about this.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • Thank you, very informative. I'm not sure what I really need at this point. The community I'm doing this for is comparatively small, so I could technically get away with just addBCC. However, the fact the number of members fluctuates and different mail services have different limits kind of bothers me. I don't think I can set up an MTA as the server is private and I have no direct access to it. So, the only other option is using PHPMailer alone to send individual emails, which might get real slow as the member list will grow... so ultimately it's either losing mails or suffer low performance. – Puddintane Jan 26 '22 at 16:50
  • I don’t think you really need to worry that much about performance. Email delivery is typically bottlenecked by receivers rather than senders (often deliberately). FWIW I use PHPMailer at send rates up to about 250 messages *per second*, but there’s little chance of receivers accepting that kind of rate - and your own mail server in the middle means you can just leave it to get on with it. – Synchro Jan 26 '22 at 19:54