title: using wp_cron to send bulk mail with wp_mail
body: <p>I have posted this question over at wordpress answers, but after several days have received no response, so I thought I would post it over here.  </p>
  
  <p>I am working on a custom plugin to send recurring invoices on the 1st of every month. So far, the plugin is working pretty well. It creates new invoices for each user based on their order and does send 1 email. But it only sends 1 email to the last email address from the query, not to every user. So here is my current function, maybe someone can see something I could do to make this send an individual email to each user with wp_mail.</p>
  
  <p>Note** This is in testing phase, so I am using the exact date in the IF statement at the beginning to see the emails and invoices be created...</p>
  
  <pre><code>function mps_send_annual_notification()  {
  
  // START BY CHECKING THE DATE... 
  // WE ONLY WANT TO RUN THIS ON THE 1ST OF EACH MONTH
  
  date_default_timezone_set('America/Chicago');
  $cron_date = date('m-d');
  
  // BEGIN DATE VERIFICATION
  if ('11-21' == $cron_date) {
  
  // GET THE USERS AND INFO FOR RECURRING ORDERS  
  global $post;
  $args = array( 
  'post_type' =&gt; 'purchase',
  'posts_per_page' =&gt; -1,
  'meta_query' =&gt; array(
      array (
      'key' =&gt; 'purchase_cycle',
      'value' =&gt; 'Annually Recurring',
      'compare' =&gt; '='
      )
  )
  );
  
  $myposts = get_posts( $args );
  foreach( $myposts as $post ) :  setup_postdata($post);
  
  $client = get_post_meta($post-&gt;ID, 'purchase_user', true);
  $email = get_post_meta($post-&gt;ID, 'purchase_user_email', true);
  $service = get_post_meta($post-&gt;ID, 'purchase_service', true);
  $price = get_post_meta($post-&gt;ID, 'purchase_price', true);
  
  $timestamp = date('Ymd');
  $today = date('m-d-Y');
  
  // CREATE A TITLE BASED ON USER NAME, THE MEMBERSHIP TYPE, AND THE DATE
  
  // Turn User Name into Initials
  $words = explode(' ', $client);
  $acronym = '';
  
  foreach ($words as $w) {
  $acronym .= $w[0];
  }
  
  // Turn Service into Initials
  $svc = explode(' ', $service);
  $svc_letters = '';
  foreach ($svc as $value) {
  $svc_letters .= $value[0];
  }
  
  $title_date = date('YmdHis');
  
  $invoice_title = $acronym.$title_date.$svc_letters;
  
  // CREATE AN INVOICE
  
  $invoicepost = array(
  'post_status' =&gt; 'publish', 
  'post_title' =&gt; $invoice_title,
  'post_type' =&gt; 'invoice', 
  'post_author' =&gt; 1);  
  remove_action( 'mps_cron_hook_example', 'mps_update_message');
  $pid = wp_insert_post( $invoicepost );
  
  // ADD THE INVOICE META
  add_post_meta($pid, 'invoice_user', $client, true); 
  add_post_meta($pid, 'invoice_user_email', $email, true); 
  add_post_meta($pid, 'invoice_date', $today, true); 
  add_post_meta($pid, 'invoice_due_date', $today, true); 
  add_post_meta($pid, 'invoice_timestamp', $timestamp, true); 
  add_post_meta($pid, 'invoice_total_due', $price, true);
  add_post_meta($pid, 'invoice_balance', $price, true);
  add_post_meta($pid, 'invoice_description', 'Annual Membership Dues', true);
  add_post_meta($pid, 'invoice_type', 'Dues', true);
  add_post_meta($pid, 'invoice_name', $service, true);
  
  
  // SET MAIL FILTERS
  add_filter ("wp_mail_content_type", "cron_invoice_mail_content_type");
  function cron_invoice_mail_content_type() {
  return "text/html";
  }
  
  add_filter ("wp_mail_from", "cron_invoice_mail_from");
  function cron_invoice_mail_from() {
  return "no-reply@example.com";
  }
  
  add_filter ("wp_mail_from_name", "cron_invoice_mail_from_name");
  function cron_invoice_mail_from_name() {
  return "Website Name Goes Here";
  }
  
  // CREATE EMAIL MESSAGE
  $email_subject = 'You Have a New Invoice';
  
   ob_start(); ?&gt;
  
  &lt;html&gt;
  &lt;head&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  &lt;p&gt;
      Hi &lt;?php echo $client; ?&gt;!
  &lt;/p&gt;
  &lt;p&gt;
      Message to user goes here...
  &lt;/p&gt;
  
  &lt;p&gt; Invoice Number:  &lt;?php echo $invoice_title; ?&gt;&lt;br /&gt;
      Invoice Date:  &lt;?php echo $today; ?&gt;&lt;br /&gt;
      Invoice Due Date:  &lt;?php echo $today; ?&gt;&lt;br /&gt;
      Account Status:  &lt;?php echo $service; ?&gt;&lt;br /&gt;
      Amount Due:  &lt;?php echo $price; ?&gt;&lt;br /&gt;
  &lt;/p&gt;
  
  &lt;p&gt; &lt;a href="link to invoice"&gt;View your invoice online.&lt;/a&gt;
  
  &lt;p&gt;
      Sincerely,&lt;br /&gt;
      Website Name Goes Here
  &lt;/p&gt;
  &lt;p&gt;
  &lt;center&gt;This message was automatically generated by the online account management    system.&lt;/center&gt;
  &lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  &lt;?php // SEND THE MESSAGE
  $message = ob_get_contents();
   ob_end_clean();
  
  wp_mail($email, $email_subject, $message);  
  
  // end the beginning query
  endforeach;
  
  } // END OF DATE VERIFICATION
  }
  </code></pre>