我有一个自定义的帖子类型“月报”,需要在发布新公告时向订阅者发送一封盲邮件。
下面的代码可以发送给订阅者,但所有人都可以看到电子邮件。
如何将代码修改为密件抄送订户?
add_action( \'transition_post_status\', \'send_mails_on_publish\', 10, 3 );
function send_mails_on_publish( $new_status, $old_status, $post )
{
if ( \'publish\' !== $new_status or \'publish\' === $old_status
or \'monthlybulletin\' !== get_post_type( $post ) )
return;
$subscribers = get_users( array ( \'role\' => \'subscriber\' ) );
$emails = array ();
foreach ( $subscribers as $subscriber )
$emails[] = $subscriber->user_email;
$body = sprintf( \'Hey there is a new entry!
See <%s>\',
get_permalink( $post )
);
wp_mail( $emails, \'New entry!\', $body );
}
谢谢大家!
最合适的回答,由SO网友:websupporter 整理而成
第四个参数为wp_mail()
您可以扩展电子邮件的标题。所以you can use BCC there. 这应该可以:
<?php
$bcc = \'Bcc: \';
$i = 0;
foreach( $emails as $email ){
if( $i != 0 )
$bcc .= \', \';
$bcc .= $email;
$i = 1;
}
$headers[] = $bcc;
wp_mail( \'[email protected]\', \'New entry!\', $body, $headers );
?>