使用wp cron发送带有wp_mail的批量电子邮件

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.

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.

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...

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' => 'purchase',
'posts_per_page' => -1,
'meta_query' => array(
    array (
    'key' => 'purchase_cycle',
    'value' => 'Annually Recurring',
    'compare' => '='
    )
)
);

$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post);

$client = get_post_meta($post->ID, 'purchase_user', true);
$email = get_post_meta($post->ID, 'purchase_user_email', true);
$service = get_post_meta($post->ID, 'purchase_service', true);
$price = get_post_meta($post->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' => 'publish', 
'post_title' => $invoice_title,
'post_type' => 'invoice', 
'post_author' => 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(); ?>

<html>
<head>

</head>
<body>
<p>
    Hi <?php echo $client; ?>!
</p>
<p>
    Message to user goes here...
</p>

<p> Invoice Number:  <?php echo $invoice_title; ?><br />
    Invoice Date:  <?php echo $today; ?><br />
    Invoice Due Date:  <?php echo $today; ?><br />
    Account Status:  <?php echo $service; ?><br />
    Amount Due:  <?php echo $price; ?><br />
</p>

<p> <a href="link to invoice">View your invoice online.</a>

<p>
    Sincerely,<br />
    Website Name Goes Here
</p>
<p>
<center>This message was automatically generated by the online account management    system.</center>
</p>
</body>
</html>

<?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
}

You need to have a loop straight after you have queried the users you're sending to. Inside that loop, you will need to declare your variables for the message and also send the email. This way your stepping through and sending for each user separately.