简体   繁体   中英

PHPMailer: Batch mailing using addBCC()

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM