wp_mail()发送双重内容

时间:2016-01-20 13:11:18

标签: php wordpress function email

此功能可获取自定义的帖子类型内容,并将其作为电子邮件与一些广告(也是自定义帖子类型)一起发送。

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\n";
    $mailheaders .= "X-Priority: 1\n";
    $mailheaders .= "Content-Type: text/html; charset=\"UTF-8\"\n";
    $mailheaders .= "Content-Transfer-Encoding: 7bit\n\n";
    $mailheaders .= "From: $from_name <$from_email>" . "\r\n";

        $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();
}

然后我在发布jobs_newsletter自定义帖子类型

上运行该功能
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);

这一切都有效,但并非如此。我收到双重内容的邮件,有时是三倍。

邮件内容的示例:

您好,

以下是您可能感兴趣的一些工作

职位:→前端开发人员
公司:公司(世界)
申请至:21/02

您可以在http://example.com/jobs/上看到所有招聘广告。

邮件内容的示例:

以下是您可能感兴趣的一些工作

职位:→前端开发人员
公司:公司(世界)
申请至:21/02

您可以看到http://example.com/jobs/.Here上的所有招聘广告都是您可能感兴趣的工作

职位:→前端开发人员
公司:公司(世界)
申请至:21/02

您可以在http://example.com/jobs/上看到所有招聘广告。

1 个答案:

答案 0 :(得分:2)

您需要在while循环的开头清除If Dir("C:\Users\" & sUserName & "\Desktop\DataDump\") = "" Then MkDir "C:\Users\" & sUserName & "\Desktop\DataDump\" Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets If ws.Name = MyCell.Value Then ws.SaveAs "C:\Users\" & sUserName & "\Desktop\DataDump\DataDump.csv", xlCSV Workbooks.Open Filename:="C:\Users\" & sUserName & "\Desktop\DataDump\DataDump.csv" Cells.Select Selection.Delete ActiveWorkbook.Close True LastRowCusip = ActiveSheet.Cells(ActiveSheet.Rows.Count, "BW").End(xlUp).Row Set Rng = Range("BG5:BW" & LastRowCusip) Workbooks.Open Filename:="C:\Users\" & sUserName & "\Desktop\DataDump\DataDump.csv" LR = Cells(Rows.Count, "A").End(xlUp).Row + 3 'last used row Rng.Copy Range("A" & LR) ActiveWorkbook.Close True End If Next End If ,否则它将为初始sUserName = Environ$("username") 选择的每个候选重建消息正文,并将其附加到先前发送的消息

$emailBody