plugins – Let post type to accept only original mails not replies

Question

I have the following code which inserts all the mails. I want only the original mail to be inserted into the post type, not the replies.

function checkEmailAlreadyExists( $message_id ) {
    $posts_with_meta = get_posts( array(
        'posts_per_page' => 1, // we only want to check if any exists, so don't need to get all of them
        'post_type'      => 'faqpress-tickets',
        'meta_key'       => 'ticket_id',
        'meta_value'     => $message_id,
        'fields'         => 'ids', // we don't need it's content, etc.
    ) );

    return ( count( $posts_with_meta ) ) ? true : false;    
}
$total = $emails->total_msg(); 

for ( $j=1; $j <= $total; $j++ ) {
    $mail = $emails->get( $j );
    if ( !empty( $mail['header']->message_id ) && !checkEmailAlreadyExists( $mail['header']->message_id )  ) {
        $post_array = array( 
            'post_content'  => $mail['body'], 
            'post_title'    => $mail['header']->subject,
            'post_type'     => 'faqpress-tickets',
            'post_author'   =>  ucwords(strstr($mail['header']->fromaddress, '<',true)),
            'post_status'   => 'publish',
            'meta_input'    => array(   
                'User'      => ucwords(strstr($mail['header']->fromaddress, '<',true)),
                'From'      => preg_replace('~[<>]~', '', strstr($mail['header']->fromaddress, '<')),
                'Email'     => $mail['header']->Date,
                'ticket_id' => $mail['header']->message_id,   
            ),
        );
      wp_insert_post( $post_array );
    }
}   

The original email has a unique message_id and the reply message has a reference_id that is the same as the message_id of the original message.

0
Rohit KC 11 months 2022-01-04T06:08:53-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse