wp_mail recipient array not sending?

All we need is an easy explanation of the problem, so here it is.

I am using wp_mail to send an email to multiple recipients.

my mail function looks like this:

wp_mail($group_emails, 'my subject', 'my message', $headers);

$group_emails is an array of email address’s and gets outputed like this:

$group_emails = Array ( [0] => [email protected] [1] => [email protected] [2] => [email protected] [3] => [email protected] [4] => [email protected] [5] => [email protected] [6] => [email protected] [7] => [email protected] [8] => [email protected] [9] => [email protected] )

For some reason the email does not get sent to the above emails? If i remove the multiple recipients and just put a single email address, it works fine!

Any suggestions?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

There are multiple ways of doing this.

You can consider any of the following.

1.My preferred:

foreach($group_emails as $email_address)
{
   wp_mail($email_address, 'my subject', 'my message', $headers);
}

2.Another way

Define the array as follows.

$group_emails = array('[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]' );

And then try your regular procedure:

wp_mail($group_emails, 'my subject', 'my message', $headers);

I am not sure about the second way. But the first way will work for sure.

Method 2

I want to add to what @Rohit has said, you can also send multiple recipients as a comma-separated string.

From the Codex

<?php wp_mail( $to, $subject, $message, $headers, $attachments ); ?> 

Parameters

$to
(string or array) (required) The intended recipient(s). Multiple recipients may be specified using an array or a comma-separated string.

Default: None

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply