2016-01-20 123 views
0

这是一种可以自定义帖子类型内容并将其作为电子邮件以及一些广告(也是自定义帖子类型)发送的功能。wp_mail()发送双倍内容

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 = '[email protected]'; 

    $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/招聘广告。

+0

我认为你必须 - $消息= ''。 $ emailBody。''; wp_mail($ email,$ subject,$ message,$ mailheaders);在这段时间后写这个内容 – WisdmLabs

+0

它不会工作,因为它必须发送邮件给每个候选人,这就是它必须在循环内 –

+0

$ emailBody clear这个内容在循环的开始 – WisdmLabs

回答

2

您需要在while循环的开始处清除$emailBody,否则它将重建由初始$query选择的每个候选者的消息正文,并将其附加到先前发送的消息。

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

它的工作。非常感谢! –