0

The context

On my site I have a custom post type (project) where visitors can subscribe to a post and get various notifications via email. My site is hosted locally right now and I am using Mailhog for testing the emails.

The problem

I am trying to send an email to each individual subscriber of a post - when that post is deleted (trashed). For example if there are x8 subscribers of a post then x8 emails should be sent - one email for each subscriber.

I cannot seem to pass the array of subscribers ($all_subscriber_emails) correctly to generate the individual emails? What could I be doing incorrectly or not understanding? Thanks in advance for any help :-)

  • Note that the $all_subscriber_emails variable outputs the subscriber list in the following format - '[email protected]', '[email protected]', '[email protected]' etc.... What I don't understand is that this exactly matches the static array format that works perfectly well?

What I have so far

function user_project_deleted_notice( $post_id ) {

    $post = get_post($post_id);
    $subject = 'The Project: '.$post->post_title.' @'.get_bloginfo('name').' has just been deleted';
    $headers = array('Content-Type: text/html; charset=UTF-8');

    $all_subscriber_emails ='';

    // Get the subscribers
    $posts = get_posts(array(
        'post_type'   => 'project_subscriber',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'fields' => 'ids',
        'meta_query' => array(
            array(
                'key' => 'project',
                'value' => $post_id,
                'compare' => 'EXISTS'
            ),
        ),
    ));

    $subscriber_email = array();

    // Loop over each post & get subscriber emails
    foreach($posts as $thepost){

        $subscriber_email[] = get_post_meta($thepost,"pl_subscriber_email",true);

        // Generate comma seperated email list in single quotes
        $all_subscriber_emails = "'" . implode("', '", $subscriber_email) . "'";
        
        $sub_name = get_post_meta($thepost, 'pl_subscriber_name', true);
       
    }
    // Create the email array so that we can send 'individual emails' to each subscriber
    
    $email_array = array($all_subscriber_emails); // I get nothing from this. No emails at all?
    
    // $email_array = array('[email protected]', '[email protected]', '[email protected]'); // Static array works perfectly but my subscriber list is not

    // $email_array = array($subscriber_email); // Works but creates large list of all emails in one email. So not what I want.

    $offer_msg ='
    <p>Unfortunately, this project has been removed by the creator.</p> 
    <p>You can always <a href="'.esc_url(get_home_url()).'/subscribe/">subcribe to other projects</a> on our platform.</p>';

    ob_start();
    include('email-templates/email-header.php');

    print'
    <p style="font-size:18px;">Hi <strong>'.$sub_name.'</strong>,</p>
    <p>You recently subscribed to the project: <strong>'.$post->post_title.'</strong>.</p>'
    .$offer_msg.'
    <p>Regards,</p>
    <p><strong>The Team</strong></p>';

    include('email-templates/email-footer.php');
    $message = ob_get_contents();
    ob_end_clean();

    // Send email to all subscribers
    foreach ($email_array as $emails){
        wp_mail( $emails, $subject, $message, $headers); // user email
    }    
}

add_action( 'wp_trash_post', 'user_project_deleted_notice' );

1 Answer 1

0

Just use the $subscriber_email[] array in the foreach() loop at the bottom of your function (where you are sending out the emails to all subscribers). It's not necessary to do any of the other data manipulations, like the implode() or incorrectly creating the email array from a string.

1
  • Thanks @ScottM. I couldn't see the wood from the trees.
    – dekkyd
    Commented May 1 at 8:37

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