使用wp_cron使用wp_mail发送批量邮件

时间:2013-11-25 07:10:10

标签: php wordpress email cron

我已经在wordpress的答案上发布了这个问题,但是几天之后没有得到回应,所以我想我会把它发布在这里。

我正在制作一个自定义插件,以便在每个月的1号发送定期发票。到目前为止,该插件工作得很好。它根据订单为每个用户创建新发票,并发送1封电子邮件。但它只向查询中的最后一个电子邮件地址发送一封电子邮件,而不是每个用户。所以这是我当前的功能,也许有人可以看到我可以做的事情,使用wp_mail向每个用户发送个人电子邮件。

注意**这是在测试阶段,因此我在开头的IF语句中使用确切日期,以查看是否创建了电子邮件和发票......

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
}

1 个答案:

答案 0 :(得分:1)

在查询要发送给的用户后,您需要直接循环。在该循环中,您需要为消息声明变量并发送电子邮件。通过这种方式,您可以单独为每个用户单步执行。

相关问题