wp_mail()发送双重内容

This is function that takes custom post type content and sends it as an email along with some ads (also custom post type).

function send_newsletter_without_area($ID, $post_obj){

    global $post;

    $mail_text = $post_obj->post_content;

    // ad types for listing in mail
    $ad_types = array(
        'premium'  => 'Premium headline',
        'standard' => 'Standard headline',
    );

    $subject    = 'Latest jobs';
    $from_name  = 'Admin';
    $from_email = 'example@mail.com';

    $mailheaders  = "MIME-Version: 1.0
";
    $mailheaders .= "X-Priority: 1
";
    $mailheaders .= "Content-Type: text/html; charset=\"UTF-8\"
";
    $mailheaders .= "Content-Transfer-Encoding: 7bit

";
    $mailheaders .= "From: $from_name <$from_email>" . "
";

        $args = array(
            'post_type'      => 'candidates',
            'posts_per_page' => -1,
            'post_status'    => 'publish',
        );

    $query = new WP_Query($args);
    while( $query->have_posts() ) : $query->the_post();

            // email missing
        if( '' == $email = get_post_meta($post->ID, 'candidate_mail', true) ){
            continue;
        }

        $job_area = wp_get_object_terms($post->ID, 'oznaka');

        if ( empty($job_area) ) {

            $emailBody .= $mail_text  . '<br /><br />';

            foreach ( $ad_types as $ad_key => $ad_headline ){

                $o_args = array(
                            'post_type'      => 'adverts',
                            'post_status'    => 'publish',
                            'posts_per_page' => -1,
                            'meta_query'     => array(
                                'relation' => 'AND',
                                array(
                                    'key'   => 'ad_type',
                                    'value' => $ad_key,
                                ),
                            ),   
                        );
                $o_query = new WP_Query($o_args);
                if( $o_query->have_posts() ){
                    while( $o_query->have_posts() ) : $o_query->the_post();
                            $emailBody .= '<b>Position:</b> → <a href="'. get_permalink() .'?psl=nwsl" target="_blank">'. get_the_title() .'</a>';
                            $emailBody .= '<br />';
                            $emailBody .= '<b>Company:</b> <a href="' . get_field('company_website') . '" target="_blank">' . get_field('company_name') . '</a> (' . get_field('location') . ')<br />';

                            $exp_date = DateTime::createFromFormat('Ymd', get_field('exp_date')); 
                            $exp_date = $exp_date->format('d/m');   
                            $emailBody .= 'Apply until: ' . $exp_date . '<br /><br />';

                    endwhile;
                }  // if( $o_query->have_posts() )
            } // foreach( $ad_types as $ad_key => $ad_headline )

            wp_reset_postdata();

        $emailBody .= '<br /><br />You can see all job ads on http://example.com/jobs/.';   

        }

    $message = '<html><head></head><body>'. $emailBody .'</body></html>';
    wp_mail($email, $subject, $message, $mailheaders);

    endwhile;

    wp_reset_postdata();
}

I then run that function on publishing jobs_newsletter custom post type

function run_when_jobs_newsletter_published($ID, $post) {

send_newsletter_sa_oznakama($ID, $post);

}
add_action('publish_jobs_newsletter', 'run_when_jobs_newsletter_published', 10, 2);

And this all works, but not as it should. I get mail with double content, sometimes triple.


Example of how mail content should look like:

Hello,

Here are some jobs you may find interesting

Position: → Frontend Developer
Company: Company (World)
Apply until: 21/02

You can see all job ads on http://example.com/jobs/.

Example of how mail content looks like:

Here are some jobs you may find interesting

Position: → Frontend Developer
Company: Company (World)
Apply until: 21/02

You can see all job ads on http://example.com/jobs/.Here are some jobs you may find interesting

Position: → Frontend Developer
Company: Company (World)
Apply until: 21/02

You can see all job ads on http://example.com/jobs/.

You need to clear $emailBody at the beginning of your while loop, otherwise it will rebuild the body of the message for each candidate selected by the initial $query, and append it to the previously sent message.

...
while( $query->have_posts() ) : 
    $emailBody = '';
    $query->the_post();
...