WordPress

php — Wysyłaj wiele e-maili do poszczególnych subskrybentów, gdy post CPT zostanie usunięty (wyrzucony do kosza)

  • 27 kwietnia, 2024
  • 3 min read
php — Wysyłaj wiele e-maili do poszczególnych subskrybentów, gdy post CPT zostanie usunięty (wyrzucony do kosza)


Na mojej stronie mam niestandardowy typ postu (projekt), w którym odwiedzający mogą subskrybować post i otrzymywać różne powiadomienia e-mailem. Moja witryna jest obecnie hostowana lokalnie i do testowania e-maili używam programu Mailhog.

Próbuję wysłać e-mail do każdego abonent indywidualny wpisu – kiedy ten post zostanie usunięty (przeniesiony do kosza). Na przykład, jeśli post ma subskrybentów x8, należy wysłać e-maile x8 – po jednym e-mailu na każdego subskrybenta.

Nie mogę poprawnie przekazać tablicy subskrybentów ($all_subscriber_emails), aby wygenerować plik indywidualne e-maile? Co mogę robić nieprawidłowo lub czego nie rozumiem? Z góry dziękuję za jakąkolwiek pomoc 🙂

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


Źródło

Warto przeczytać!  Jak włączyć automatyczne aktualizacje w WordPress dla głównych wersji