Sending e-mail to multiple recipients via SMTP in WordPress
First published on July 24, 2009
By default, e-mails sent from within a WordPress installation use the wp_mail() function (find it in wp-includes/pluggable.php), which in turn uses PHP’s mail() function. Therefore, e-mail is sent using whatever is configured for your server.
You can specify to WordPress that you want to use a particular SMTP server to send e-mail. Plugins such as the aptly named WP Mail SMTP enable you to specify this, including a username and password if required.
However, up to and including WordPress 2.9, if you send mail via SMTP, it does not allow you to e-mail multiple recipients in the “to” field. Most users don’t ever have to deal with this limitation, but if you’re using WordPress in one of several ways (such as with a collaboration workflow), this can cause frustration.
Luckily, the wp_mail() function can be overridden. Specifically, you want to replace this code:
// Set destination address
$phpmailer->AddAddress( $to );
With this:
// Set destination address(es)
// Accept either a comma-separated list or an array
$to = explode( ',', implode( ',', (array) $to ) );
foreach ( $to as $to_recipient ) {
$phpmailer->AddAddress( trim( $to_recipient ) );
}
The best practice in such a case is to use a plugin so that whenever you upgrade WordPress, you won’t lose changes. Here is an example of such a plugin, which copies the default wp_mail() function and makes the change as detailed above. To install it, just rename the extension to “php”, put it in your WordPress plugins folder, and activate it.
July 25th, 2009 at 1:12 am
Roger says:
Thanks Peter. As a new but rather techy WordPress user, I am both delighted and overwhelmed by its myriad features and intricacies. So such little gems are always useful to collect
Roger
February 18th, 2010 at 12:39 pm
Eddie says:
Thanks, that worked great! It is hard to believe that we would have to wait for WP 3.0 to get this fixed.
Question: is there a way to modify the code and send mail one recipient at a time, one recipient per TO: ?
Reply from Peter: Yes, you could modify the wp_mail code further, but a cleaner and more maintainable way is for you to change the code that calls the wp_mail function — on a general basis, this means looping through the addresses you have and calling the function once per address. (For my use, I wanted all “to” addresses to be displayed so that everybody knew who else was being e-mailed.)
March 25th, 2010 at 12:47 pm
Stacey says:
HI,
Is there a way to included the post content in the email? We want people to be able to review the post via their mobile email.
Reply from Peter: I’m assuming that you’re referring to the collaboration e-mails plugin? You could certainly edit the pce_pending() function in the plugin file and output different aspects of the post as available in the $pce_object variable.
July 9th, 2010 at 6:55 am
Dan Stramer says:
Thanks alot peter, that fixed my problem.
By the way – the plugin link does not work
Dan
Reply from Peter: Thanks for pointing that out. I’ve fixed the link now.