dravpuso44681 2016-01-20 13:11
浏览 48
已采纳

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/.

  • 写回答

1条回答 默认 最新

  • douzhongjiu2263 2016-01-20 13:42
    关注

    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();
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致