0

I have a custom post type "monthly bulletin" and need to send a blind email to subscribers when a new bulletin is published.

The code below works to send to subscribers, but emails are visible to all.

How do I amend the code to BCC subscribers?

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 );

}

Thank you!

0

with the 4th parameter of wp_mail() you can extend the header of the Email. So you can use BCC there. This should work:

<?php
    $bcc = 'Bcc: ';
    $i = 0;
    foreach( $emails as $email ){
        if( $i != 0 )
            $bcc .= ', ';
        $bcc .= $email;
        $i = 1;
    }

    $headers[] = $bcc;
    wp_mail( 'admin@mysite.com', 'New entry!', $body, $headers );
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.