Title: Sending multiple emails
Last modified: January 2, 2017

---

# Sending multiple emails

 *  Resolved [Elliot Taylor](https://wordpress.org/support/users/raisonon/)
 * (@raisonon)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/)
 * Hi,
 * How can I send ten unique emails using wp_mail? I don’t want the other recipients
   to see each other.
 * I currently have an array of the user_emails. Should I loop through them and 
   trigger wp_mail for each one or is there a better practice?
 * Currently I have
 * wp_mail( $user_email_addresses, $subject, $message, $headers );
 * where user_email_addresses is an array of email addresses
 * I could bcc but I thought that would not be good practice.
 * Thanks!
 * Elliot

Viewing 15 replies - 1 through 15 (of 19 total)

1 [2](https://wordpress.org/support/topic/sending-multiple-emails-2/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/sending-multiple-emails-2/page/2/?output_format=md)

 *  Plugin Author [Mailgun](https://wordpress.org/support/users/mailgun/)
 * (@mailgun)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8614132)
 * Hey [@raisonon](https://wordpress.org/support/users/raisonon/), the best way 
   to do this would be to provide all of the addresses in the `To` field, but also
   provide recipient variables through the headers field.
 * So, I think the best way to do this would be to keep a count and assign each 
   of the recipients an “id” that you don’t even necessarily have to use.
 * Example:
 *     ```
       $rcpt_vars = array();
       $idx = 0;
       foreach ($user_email_addresses as $user_addr) {
         $rcpt_vars[$user_addr] = array("batch_msg_id" => $idx);
         $idx++;
       }
   
       $rcpt_vars_json = json_encode($rcpt_vars);
   
       wp_mail($user_email_addresses, $subject, $message, array(
         "X-Mailgun-Variables: ${rcpt_vars_json}"
       ));
       ```
   
 * See also: [Sending Batch Messages](https://documentation.mailgun.com/user_manual.html#batch-sending)
 * Hope this helps! 🙂
 *  Thread Starter [Elliot Taylor](https://wordpress.org/support/users/raisonon/)
 * (@raisonon)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8614294)
 * Hi [@mailgun](https://wordpress.org/support/users/mailgun/)
 * Ok, I’ve implemented this and when I look at Mailgun logs I can see when I am
   running this with two email addresses:
 *     ```
       "recipient-domain": "raison.co",
         "method": "http",
         "campaigns": [],
         "user-variables": {
           "elliot@abc.co": {
             "batch_msg_id": 1
           },
           "ejntaylor@abc.com": {
             "batch_msg_id": 0
           }
         },
         "flags": {
           "is-routed": false,
           "is-authenticated": true,
           "is-system-test": false,
           "is-test-mode": false
         },
         "log-level": "info",
         "id": "_czIE6OkRPeQ_xVhoYX9iQ",
         "message": {
           "headers": {
             "to": "ejntaylor@abc.com, elliot@abc.co",
       ```
   
 * So I can see the user-variables are sent ok and received. But if you look at 
   the message: header” to you will see it is still sending to both the addresses
   still.
 * Any ideas appreciated. I have posted full code below:
 *     ```
       	// email vars
       	$subject = $title;
   
       	// set headers
       	$site_email = $site['post_email'];
       	$site_name = $site['post_title'];
       	$site_url = $site['site_url'];
   
       	// mailgun multiple emails
       	// https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8614132
       	$rcpt_vars = array();
       	$idx = 0;
       	foreach ($user_email_addresses as $user_addr) {
       		$rcpt_vars[$user_addr] = array("batch_msg_id" => $idx);
       		$idx++;
       	}
       	$rcpt_vars_json = json_encode($rcpt_vars);
   
       	// headers
       	$headers = "X-Mailgun-Variables: ${rcpt_vars_json}" . "\r\n";
       	$headers .= "From: " . $site_name ." <".$site_email.">" . "\r\n";
       	$headers .= "MIME-Version: 1.0" . "\r\n";
       	$headers .= "Content-type: text/html; charset=".get_bloginfo('charset') . "\r\n";
   
       	var_dump($headers);
   
       	// pass vars by using ob
       	ob_start();
       	include(locate_template('/_includes_templates/email_templates/default.php'));
       	$message = ob_get_clean();
   
       	// action
       	wp_mail( $user_email_addresses, $subject, $message, $headers );
       ```
   
 * Var_dump of headers:
 * `string(198) "X-Mailgun-Variables: {"ejntaylor@abc.com":{"batch_msg_id":0},"elliot@
   abc.co":{"batch_msg_id":1}} From: yogrow.co MIME-Version: 1.0 Content-type: text/
   html; charset=UTF-8 "`
    -  This reply was modified 9 years, 7 months ago by [Elliot Taylor](https://wordpress.org/support/users/raisonon/).
      Reason: email included by mistake
 *  Thread Starter [Elliot Taylor](https://wordpress.org/support/users/raisonon/)
 * (@raisonon)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8614364)
 * Hi [@mailgun](https://wordpress.org/support/users/mailgun/)
 * There are some other people with a similar problem here: [Stackoverflow Link](https://stackoverflow.com/questions/37948729/mailgun-smtp-batch-sending-with-recipient-variables-shows-all-recipients-in-to-f?rq=1)
 * The instruction (from Mailgun) seems to be to add %recipient% in the to field.
   However when I do this I see no log of this in Mailgun.
 * Thanks for looking into this.
 *  Plugin Author [Mailgun](https://wordpress.org/support/users/mailgun/)
 * (@mailgun)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8614395)
 * [@raisonon](https://wordpress.org/support/users/raisonon/) Try adding `%recipient.
   batch_msg_id%` somewhere in your email template and try sending again.
 *  Thread Starter [Elliot Taylor](https://wordpress.org/support/users/raisonon/)
 * (@raisonon)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8614458)
 * Added this in the footer:
 * `<p>Mailgun Param: %recipient.batch_msg_id%</p>`
 * No luck and the param not completed.
 * Screenshots:
    [https://www.dropbox.com/s/qmlpf893zfnk61g/Screenshot%202017-01-03%2018.23.01.png?dl=0](https://www.dropbox.com/s/qmlpf893zfnk61g/Screenshot%202017-01-03%2018.23.01.png?dl=0)
 *  Plugin Author [Mailgun](https://wordpress.org/support/users/mailgun/)
 * (@mailgun)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8614789)
 * Hey [@raisonon](https://wordpress.org/support/users/raisonon/), I’m going to 
   look deeper in to this issue, I just need a little time. I think the best solution
   for you right now is to send 10 individual emails, or alternatively, use the 
   BCC field.
 * I think that sending multiple messages would be the better solution for you, 
   though.
 * I’ll let you know when I find something 🙂
 * Thanks!
 *  Thread Starter [Elliot Taylor](https://wordpress.org/support/users/raisonon/)
 * (@raisonon)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8615094)
 * Thanks for helping 🙂
 *  Plugin Author [Mailgun](https://wordpress.org/support/users/mailgun/)
 * (@mailgun)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8615106)
 * [@raisonon](https://wordpress.org/support/users/raisonon/) of course! Let me 
   know if there’s anything else we can do for you! 🙂
 *  Plugin Author [Mailgun](https://wordpress.org/support/users/mailgun/)
 * (@mailgun)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8618986)
 * Hey [@raisonon](https://wordpress.org/support/users/raisonon/)! It’s a long shot,
   but could you try [this beta-ish](https://github.com/mailgun/wordpress-plugin/archive/sjohn/batch.zip)
   version for the batch mailing?
 * Batch mailing with the beta is like this:
 *     ```
       add_filter('mg_use_recipient_vars_syntax', 'mg_true');
       function mg_true() {
         return true;
       });
   
       wp_mail($to, $subject, $message, null, null);
   
       remove_filter('mg_use_recipient_vars_syntax', 'mg_true');
       ```
   
 * I think this should work because it will set the `To` address *after* the initial
   call to the built-in `wp_mail` filters, which is likely what is causing batch
   messages with `%recipient%` as the `To` address to not get submitted. Please 
   let me know if this ends up working 🙂
    -  This reply was modified 9 years, 7 months ago by [Mailgun](https://wordpress.org/support/users/mailgun/).
 *  Thread Starter [Elliot Taylor](https://wordpress.org/support/users/raisonon/)
 * (@raisonon)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8619137)
 * I’m getting the following error:
 * `Fatal error: Call to undefined function apply_filter() in /nas/content/live/
   superscribe/wp-content/plugins/mailgun/includes/wp-mail.php on line 285`
 * Here is my code using the above – can you confirm I have this correct:
 *     ```
       	// email vars
       	$subject = $title;
   
       	// set headers
       	//var_dump($site);
       	$site_email = $site['post_email'];
       	$site_name = $site['post_title'];
       	$site_url = $site['site_url'];
   
       	// mailgun multiple emails
       	// https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8614132
       	$rcpt_vars = array();
       	$idx = 0;
       	foreach ($user_email_addresses as $user_addr) {
       		$rcpt_vars[$user_addr] = array("batch_msg_id" => $idx);
       		/*
       						$rcpt_vars[$user_addr] = array(
       					"batch_msg_id" => $idx,
       					"email" => $user_addr);
       		*/
   
       		$idx++;
       	}
       	$rcpt_vars_json = json_encode($rcpt_vars);
   
       	// headers
       	$headers = "X-Mailgun-Variables: ${rcpt_vars_json}" . "\r\n";
       	$headers .= "From: " . $site_name ." <".$site_email.">" . "\r\n";
       	$headers .= "MIME-Version: 1.0" . "\r\n";
       	$headers .= "Content-type: text/html; charset=".get_bloginfo('charset') . "\r\n";
   
       	var_dump($headers);
   
       	// pass vars by using ob
       	ob_start();
       	include(locate_template('/_includes_templates/email_templates/default.php'));
       	$message = ob_get_clean();
   
       	// new batch filter
       	add_filter('mg_use_recipient_vars_syntax', 'mg_true');
       	function mg_true() {
       	  return true;
       	}
   
       	// action
       	wp_mail( $user_email_addresses, $subject, $message, $headers );
   
       	// new batch remove filter
       	remove_filter('mg_use_recipient_vars_syntax', 'mg_true');
       ```
   
 *  Plugin Author [Mailgun](https://wordpress.org/support/users/mailgun/)
 * (@mailgun)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8619169)
 * Looks correct, but you should be able to go without this code:
 *     ```
       	// mailgun multiple emails
       	// https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8614132
       	$rcpt_vars = array();
       	$idx = 0;
       	foreach ($user_email_addresses as $user_addr) {
       		$rcpt_vars[$user_addr] = array("batch_msg_id" => $idx);
       		/*
       						$rcpt_vars[$user_addr] = array(
       					"batch_msg_id" => $idx,
       					"email" => $user_addr);
       		*/
   
       		$idx++;
       	}
       	$rcpt_vars_json = json_encode($rcpt_vars);
   
       	// headers
       	$headers = "X-Mailgun-Variables: ${rcpt_vars_json}" . "\r\n";
       ```
   
 * Also, the error is because I made a small typo.. Could you try again with the
   same link?
 *  Thread Starter [Elliot Taylor](https://wordpress.org/support/users/raisonon/)
 * (@raisonon)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8621290)
 * Hi [@mailgun](https://wordpress.org/support/users/mailgun/)
 * I’ve been experimenting for the last hour without luck :/
 * It sends when I remove the add_filter but when that is added I don’t see an email
   being received by the mailgun log. But it is still sending to everyone on the
   email to field as one email…
 * Interestingly, I am seeing the email (with filters added) appearing in my wp_mail
   log plugin that I use in the backend of WP.
 * [https://www.dropbox.com/s/v4olk8vtnkm0ijm/Screenshot%202017-01-05%2010.26.24.png?dl=0](https://www.dropbox.com/s/v4olk8vtnkm0ijm/Screenshot%202017-01-05%2010.26.24.png?dl=0)
 * I’m not sure if that helps narrow down the problem.
 * Any help appreciated. Feel like we are nearly there!
    -  This reply was modified 9 years, 7 months ago by [Elliot Taylor](https://wordpress.org/support/users/raisonon/).
 *  Plugin Author [Mailgun](https://wordpress.org/support/users/mailgun/)
 * (@mailgun)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8622983)
 * [@raisonon](https://wordpress.org/support/users/raisonon/) Are you using a plugin
   to try to send batch mails?
 *  Thread Starter [Elliot Taylor](https://wordpress.org/support/users/raisonon/)
 * (@raisonon)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8623033)
 * I’m coding this myself – ta
 *  Thread Starter [Elliot Taylor](https://wordpress.org/support/users/raisonon/)
 * (@raisonon)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8623174)
 * Would it help to see more of my code and/or get site access?

Viewing 15 replies - 1 through 15 (of 19 total)

1 [2](https://wordpress.org/support/topic/sending-multiple-emails-2/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/sending-multiple-emails-2/page/2/?output_format=md)

The topic ‘Sending multiple emails’ is closed to new replies.

 * ![](https://ps.w.org/mailgun/assets/icon-256x256.png?rev=2434524)
 * [Mailgun for WordPress](https://wordpress.org/plugins/mailgun/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/mailgun/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/mailgun/)
 * [Active Topics](https://wordpress.org/support/plugin/mailgun/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/mailgun/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/mailgun/reviews/)

 * 19 replies
 * 2 participants
 * Last reply from: [Mailgun](https://wordpress.org/support/users/mailgun/)
 * Last activity: [9 years, 7 months ago](https://wordpress.org/support/topic/sending-multiple-emails-2/page/2/#post-8623989)
 * Status: resolved