0

SITUATION
I have a reserve system for a restaurant and I'm trying to send data out of my wpdb to myself with the mail() function around 3pm. So I get a overview of all the reservations for the current day. Everything is working fine except for my mail() function because it just sending data of one reservation but there are 2 reservations on the current date 30-03-2017. When I echo $message_mail . '<br>'; inside the WP_loop it displays the 2 reservations correctly. But then my mail code is sending just one reservation inside the mail.

If I put the mailcode in the loop, it send 2 separate mails. That's not the intention too. I've tried the wp_mail() function, but isn't working either.

QUESTION

I just want one mail each day with all the reservation info. Can someone help me with this? Thanks in advance

<?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 = "mymail@gmail.com";
      $from_no_mail = "info@restaurant.nl";
      $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;
?>

MAIL CODE

<?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 = "info@restaurant.com";
      $from_all_reservations = "reserveringsformulier@restaurantinger.nl";
      $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

You should concatenate the messages together and then send the email outside of the loop once you've built up the entire message string you want to send. Try something like this:

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>';
  • Unfortunately your suggestion is not working. It give me a blank page. What do you mean exactly with concatenate? I don't want to manually type the messages. I want the code to look how many reservations are set on the current date. Then send a mail with the $message_mail for every reservation in the same email. So for the 2 reservations now, I'm getting one mail with for example: Name: Luc, Email: luc@gmail.com . Name: Sarah, Email: sarah@gmail.com in the mail. Not 2 separate mails. For 100 reservations, it will send a mail with 100 times the information included. – W. White Mar 30 '17 at 11:44
  • Sorry - there was a typo in my answer. By concatenating, you're using the loop to build up your message, then when it finds another row, it 'concatenates' (or adds) the result of the previous row to it. Read more here: stackoverflow.com/questions/5344047/add-to-same-string-in-loop – TomC Mar 30 '17 at 13:43
  • I would suggest using the wp_mail() - it plays nice if you add any plugins that may format the message or setup SMTP gateways. wp_mail() end up calling mail() so there should be no other impact. – CapeCoder Mar 30 '17 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.