发送邮件无法正常工作

时间:2017-03-30 作者:W. White

SITUATION<我有一个餐厅预订系统,我正试图用mail() 下午3点左右开始工作。所以我得到了当天所有预订的概览。除了我的mail() 功能,因为它只发送一个预订的数据,但在当前日期2017年3月30日有两个预订。当我echo $message_mail . \'<br>\'; 在WP\\U循环内,它正确显示了2个保留。但是我的邮件代码只在邮件中发送了一个预约。

如果我将邮件代码放入循环中,它会发送两封单独的邮件。这也不是目的。我尝试了wp\\u mail()函数,但也不起作用。

QUESTION

我只想每天发一封包含所有预订信息的邮件。有人能帮我吗?提前感谢

<?php
date_default_timezone_set(\'Europe/Amsterdam\');
    $current_date = date("d-m-Y");
    //echo $current_date . \'<br>\';

    $get_all_reservations_on_date = array(
      \'post_type\' => \'reserveringen\',
      \'meta_key\' => \'datum\',
      \'meta_value\' => $current_date,
    );

    $all_reservations_on_day_query = new WP_Query( $get_all_reservations_on_date);
    //print_r($all_reservations_on_day_query);

    //wordpress loop voor mail om 15:00 uur.
    if ( $all_reservations_on_day_query->have_posts() ) :
         //the loop
        while ( $all_reservations_on_day_query->have_posts() ) : $all_reservations_on_day_query->the_post();

            //Get all values for sending mail to owner based on date.
            $klant_naam_mail = get_post_meta($post->ID, \'klant_naam\', true);
            $email_mail = get_post_meta($post->ID, \'email\', true);
            $telnr_mail = get_post_meta($post->ID, \'telefoonnummer\', true);
            $datum_mail = get_post_meta($post->ID, \'datum\', true);
            $aantal_personen_mail = get_post_meta($post->ID, \'aantal_personen\', true);
            $opmerking_mail = get_post_meta($post->ID, \'opmerking\', true);

            //the message for the mail
            $message_mail ="Naam: $klant_naam_mail\\nEmail: $email_mail\\nTelefoonnummer: $telnr_mail\\nDatum: $datum_mail\\nAantal personen: $aantal_personen_mail\\nOpmerking: $opmerking_mail";

            echo $message_mail . \'<br>\';

         endwhile;
      //end of the loop

     wp_reset_postdata();

     else :

      $send_no_reservation_to = "[email protected]";
      $from_no_mail = "[email protected]";
      $subject_no_reservation = "Alle reserveringen van de dag";
      $message_no_reservation = "Er zijn voor deze dag geen reserveringen";
      $headers_no_mail = \'From:\' . $from_no_mail . "\\r\\n";

      mail($send_no_reservation_to,$subject_no_reservation,$message_no_reservation,$headers_no_mail);

    endif;
?>
邮件代码

<?php
    $date_time = date("d-m-Y H:i:s");
    //$date_time = date("d-m-Y 15:15:00");
    //echo $date_time . \'<br>\';

    if ($date_time == date("d-m-Y 15:15:00")){
      $to_all_reservations = "[email protected]";
      $from_all_reservations = "[email protected]";
      $subject_all_reservations = "Alle reserveringen voor $current_date";
      $message_all_reservations = $message_mail;
      $headers_all_reservations = \'From:\' . $from_all_reservations . "\\r\\n";

      mail($to_all_reservations,$subject_all_reservations,$message_all_reservations,$headers_all_reservations);

    } else {

    }
?>

1 个回复
最合适的回答,由SO网友:TomC 整理而成

您应该将消息连接在一起,然后在构建完要发送的整个消息字符串后,在循环外部发送电子邮件。尝试以下操作:

if ( $all_reservations_on_day_query->have_posts() ) :
         //the loop
$message_mail = ""; 
while ( $all_reservations_on_day_query->have_posts() ) : $all_reservations_on_day_query->the_post();

            //Get all values for sending mail to owner based on date.
            $klant_naam_mail = get_post_meta($post->ID, \'klant_naam\', true);
            $email_mail = get_post_meta($post->ID, \'email\', true);
            $telnr_mail = get_post_meta($post->ID, \'telefoonnummer\', true);
            $datum_mail = get_post_meta($post->ID, \'datum\', true);
            $aantal_personen_mail = get_post_meta($post->ID, \'aantal_personen\', true);
            $opmerking_mail = get_post_meta($post->ID, \'opmerking\', true);

            //the message for the mail
            //concatenate the messages together
            $message_mail .="Naam: $klant_naam_mail\\nEmail: $email_mail\\nTelefoonnummer: $telnr_mail\\nDatum: $datum_mail\\nAantal personen: $aantal_personen_mail\\nOpmerking: $opmerking_mail";


         endwhile;

echo $message_mail . \'<br>\';

相关推荐

Increase offset while looping

我正在编写一个自定义帖子插件,它将自定义帖子分组显示为选项卡。每组4个岗位。是否可以编写一个偏移量随每次循环而增加的查询?因此,结果将是:-第一个查询显示从1到4的帖子-第二个查询显示从5到8的帖子-第三个查询显示从9到12的帖子等。 <div class=\"official-matters-tabs\"> <?php $args = array(\'post_type\' => \'official-matters\', \'showp