PHP (WP) booking file only sending one of 2 emails

I’m having an issue with this file, in that it controls the sending of mail to the customer who is making the booking and the hotelier. Right now, it only sends mail to the customer and not to the hotelier. Is there any obvious reasons you can see for this from scanning the code below. This is a WordPress template file.

<?php 

/* 
Template Name: Booking
*/

// Fetch options stored in $data
global $qns_data;


/**
 * Sends mail by SMTP or mail(), depending on options
 *
 * @global array $data
 * @param array $from array('name' => string, 'email' => string)
 * @param array $to array('name' => string, 'email' => string)
 * @param array $replyto array('name' => string, 'email' => string)
 * @param string $subject
 * @param string $body
 */
if (!function_exists('qns_mail')) {
function qns_mail($from, $to, $replyto, $subject, $body) {
	// import options
	global $qns_data;
	// determine whether to send by SMTP
	if (!empty($qns_data['enable_smtp']) && !empty($qns_data['smtp_host']) && !empty($qns_data['smtp_port'])) {
	// convert encryption
	$smtp_encryption = '';
	switch($qns_data['smtp_encryption']) {
		case 'No encryption': $smtp_encryption = ''; break;
		case 'SSL encryption': $smtp_encryption = 'ssl'; break;
		case 'TLS encryption': $smtp_encryption = 'tls'; break;
		default: $smtp_encryption = '';
	}
	// instantiate mailer
	global $phpmailer;
	if (!is_object($phpmailer) || !is_a($phpmailer, 'PHPMailer')) {
		require_once ABSPATH . WPINC . '/class-phpmailer.php';
		require_once ABSPATH . WPINC . '/class-smtp.php';
	}
	$phpmailer = new PHPMailer(true); // clear out any previous settings
	$phpmailer->isSMTP();
	$phpmailer->Host = $qns_data['smtp_host'];
	$phpmailer->Port = $qns_data['smtp_port'];
	$phpmailer->SMTPSecure = $smtp_encryption;
	// determine whether to send with authentication
	if (!empty($qns_data['smtp_auth']) && !empty($qns_data['smtp_username'])) {
		$phpmailer->SMTPAuth = TRUE;
		$phpmailer->Username = $qns_data['smtp_username'];
		$phpmailer->Password = $qns_data['smtp_password'];
	} else {
		$phpmailer->SMTPAuth = FALSE;
	}
	$phpmailer->From = $from['email'];
	$phpmailer->FromName = $from['name'];
	$phpmailer->clearAllRecipients();
	$phpmailer->AddAddress($to['email']);
	$phpmailer->AddReplyTo($replyto['email']);
	$phpmailer->Subject = $subject;
	$phpmailer->CharSet = 'UTF-8';
	$phpmailer->Body = $body;
	$phpmailer->IsHTML(TRUE);

	// send
	try {
		$phpmailer->Send();
	} catch (Exception $ex) {
		wp_die($ex->getMessage());
	}
	unset($phpmailer);
	} else {
		$headers = <<<EOT
MIME-Version: 1.0
Content-type: text/html; charset=UTF-8
From: "{$from['name']}" <{$from['email']}>
Reply-To: {$replyto['email']}
EOT;
		mail($to['email'], $subject, $body, $headers);
	}
} // end function
} // end if

// Get booking page ID
$booking_page = $qns_data['booking_page_url'];

// Check if form has been submit
if ( $_SERVER['HTTP_REFERER'] == $qns_data['booking_page_url']  && $_POST['submit'] == 'submit' ) {
	$submit = true;
	
	// Post form data from this page
	$book_room_type = $_POST['book_room_type'];
	$book_date_from = $_POST['book_date_from'];
	$book_date_to = $_POST['book_date_to'];
	$book_full_name = $_POST['book_full_name'];
	$book_num_people = $_POST['book_num_people'];
	$book_email = $_POST['book_email'];
	$book_phone = $_POST['book_phone'];
	$book_message = $_POST['book_message'];
	$book_room_price = $_POST['book_room_price'];
	
	if(trim( $book_room_type ) === '') {
		$book_room_type_error = __('Room Type is a required field', 'qns');
		$got_error = true;
	}
	
	if(trim( $book_date_from ) === '') {
		$book_date_from_error = __('Date From is a required field', 'qns');
		$got_error = true;
	}
	
	if(trim( $book_date_to ) === '') {
		$book_date_to_error = __('Date To is a required field', 'qns');
		$got_error = true;
	}
	
	if(trim( $book_full_name ) === '' or trim( $book_full_name ) === __('Full Name','qns') ) {
		$book_full_name_error = __('Full Name is a required field', 'qns');
		$got_error = true;
	}
	
	if(trim( $book_num_people ) === '' or trim( $book_num_people ) === __('Number of People','qns') ) {
		$book_num_people_error = __('Number of People is a required field', 'qns');
		$got_error = true;
	}
	
	if(trim( $book_email ) === '' or trim( $book_email ) === __('Email Address','qns') or valid_email( trim($book_email) ) === FALSE ) {
		$book_email_error = __('Email Address is a required field', 'qns');
		$got_error = true;
	}
	
}

// If the form on this page has not been submit post the values from the accommodation page
else {
	
	if($_POST['book_room_type_and_price'] != '') {
		$book_room_price_array = explode(',', $_POST['book_room_type_and_price']);		
		$book_room_type = $book_room_price_array[0];
		$book_room_price = $book_room_price_array[1];
	}
	
	else {
		
		$book_room_type = $_POST['book_room_type'];
		$book_room_price = $_POST['book_room_price'];
		
	}
	
	$book_date_from = $_POST['book_date_from'];
	$book_date_to = $_POST['book_date_to'];
	
}

// Calculate Length of Stay
if( $qns_data['datepickerformat'] == 'mm/dd/yyyy') {
	$date_from_stt = strtotime($book_date_from);
	$date_to_stt = strtotime($book_date_to);
	$datediff = $date_to_stt - $date_from_stt;
	$total_stay = floor($datediff/(60*60*24));
} else {
	function bookDateDiff($date1,$date2) {

		$old_from = $date1;
		$arr1 = explode('/', $old_from);
		$book_date_from = $arr1[1].'/'.$arr1[0].'/'.$arr1[2];

		$old_to = $date2;
		$arr2 = explode('/', $old_to);
		$book_date_to = $arr2[1].'/'.$arr2[0].'/'.$arr2[2];

		$date_from_stt = strtotime($book_date_from);
		$date_to_stt = strtotime($book_date_to);
		$datediff = $date_to_stt - $date_from_stt;
		$total_stay = floor($datediff/(60*60*24));

		return $total_stay;

	}

	$total_stay = bookDateDiff($book_date_from,$book_date_to);
}



// Calculate Total Price
$total_price = $book_room_price * $total_stay;

// Get currency unit stored in the theme options
if ( $qns_data['currency_unit'] !== '' ) {
	$currency_unit = $qns_data['currency_unit'];
}

// If the unit is not set
else {
	$currency_unit = '$';
}

// If form has been submit and there are no errors send it
if ( $submit == true && $got_error != true ) {
	
	$email_to = $qns_data['contact_email'];

	if (!isset($email_to) || ($email_to == '') ){
		$email_to = get_option('admin_email');
	}
	
	// Send email to website admin
	$subject = htmlspecialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) . __(' Booking Form','qns');
	
	$body = '<ul style="margin: 0px;padding:0 0 0 15px;">';
	$body .= '<li style="margin-bottom: 3px;"><strong>' . __('Room Type','qns') . ":</strong> " . $book_room_type . "</li>";
	$body .= '<li style="margin-bottom: 3px;"><strong>' . __('Date','qns') . ":</strong> " . $book_date_from . " - " . $book_date_to . "</li>";
	$body .= '<li style="margin-bottom: 3px;"><strong>' . __('Guest Name','qns') . ":</strong> " . $book_full_name . "</li>";
	$body .= '<li style="margin-bottom: 3px;"><strong>' . __('Number of People','qns') . ":</strong> " . $book_num_people . "</li>";
	$body .= '<li style="margin-bottom: 3px;"><strong>' . __('Email','qns') . ":</strong> " . $book_email . "</li>";
	$body .= '<li style="margin-bottom: 3px;"><strong>' . __('Phone','qns') . ":</strong> " . $book_phone . "</li>";
	$body .= '<li style="margin-bottom: 3px;"><strong>' . __('Price Quoted','qns') . ":</strong> " . $currency_unit.$total_price . "</li>";
	$body .= '<li style="margin-bottom: 3px;"><strong>' . __('Special Requirements','qns') . ":</strong> " . $book_message . "</li>";
	$body .= '<li style="margin-bottom: 3px;"><strong>' . __('IP Address','qns') . ":</strong> " . $_SERVER['REMOTE_ADDR'] . "</li>";
	$body .= '</ul>';

	qns_mail(
		array('name' => $book_full_name, 'email' => $email_to), // from
		array('name' => '', 'email' => $email_to), // to
		array('name' => '', 'email' => $book_email), // replyto
		$subject,
		$body
	);
	$emailSent = true;
	
	// Send email to guest
	$subject2 = htmlspecialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) . ': ' . __('Reservation Received','qns');
	
	if($qns_data['accom_success_msg']) {
		$body2 = $qns_data['accom_success_msg'] . '<br /><br />';
	} else {
		$body2 = __('Booking Successful! Nice Hotel will reply within 24 hours <br /><br />','qns');
	}
	
	$body2 .= '<ul style="margin: 0px;padding:0 0 0 15px;">';
	$body2 .= '<li style="margin-bottom: 3px;"><strong>' . __('Room Type','qns') . ":</strong> " . $book_room_type . "</li>";
	$body2 .= '<li style="margin-bottom: 3px;"><strong>' . __('Date','qns') . ":</strong> " . $book_date_from . " - " . $book_date_to . "</li>";
	$body2 .= '<li style="margin-bottom: 3px;"><strong>' . __('Guest Name','qns') . ":</strong> " . $book_full_name . "</li>";
	$body2 .= '<li style="margin-bottom: 3px;"><strong>' . __('Number of People','qns') . ":</strong> " . $book_num_people . "</li>";
	$body2 .= '<li style="margin-bottom: 3px;"><strong>' . __('Email','qns') . ":</strong> " . $book_email . "</li>";
	$body2 .= '<li style="margin-bottom: 3px;"><strong>' . __('Phone','qns') . ":</strong> " . $book_phone . "</li>";
	$body2 .= '<li style="margin-bottom: 3px;"><strong>' . __('Price Quoted','qns') . ":</strong> " . $currency_unit.$total_price . "</li>";
	$body2 .= '<li style="margin-bottom: 3px;"><strong>' . __('Special Requirements','qns') . ":</strong> " . $book_message . "</li>";
	$body2 .= '</ul>';

	qns_mail(
		array('name' => str_replace('"', '\"', htmlspecialchars_decode(get_bloginfo('name'), ENT_QUOTES)), 'email' => $email_to), // from
		array('name' => '', 'email' => $book_email), // to
		array('name' => '', 'email' => $email_to), // replyto
		$subject2,
		$body2
	);
	$emailSent = true;
	
}

?>

<?php get_header(); ?>

	<?php //Display Page Header
		global $wp_query;
		$postid = $wp_query->post->ID;
		echo page_header( get_post_meta($postid, 'qns_page_header_image', true) );
		wp_reset_query();
	?>
	
	<!-- BEGIN .section -->
	<div class="section page-full clearfix">

		<h2 class="page-title"><?php _e('Room Booking','qns'); ?></h2>
		
		<!-- BEGIN .page-content -->
		<div class="page-content page-content-full">
			
			<?php // Prevent users from loading the page directly
				if ( $_SERVER['HTTP_REFERER'] == '' or $_POST['book_confirm'] != '1' ) : 
					echo '<div class="msg fail"><p>' . __('Please do not load this page directly, go to the accommodation page first and select room','qns') . '</p></div>'; 
			?>
			
			<?php else : ?>
			
			<!-- BEGIN .even-cols -->
			<div class="even-cols booking-cols clearfix">
				
				<!-- BEGIN .one-half -->
				<div class="one-half">
					
					<?php
					
						if ( $submit == true && $got_error != true ) {
							echo '<div class="msg success"><p>';
							
							if($qns_data['accom_success_msg']) {
								echo $qns_data['accom_success_msg'];
							} else {
								_e('Booking Request Successful! Prices are in $US dollars. We will reply within 24 - 48 hours. Your booking is not confirmed until we contact you.','qns');
							}
							
							echo '</p></div>';
						}
						
						if ( $got_error == true ) {
							
							echo '<div class="msg fail">
							<ul class="list-fail">';
							
							if ( $book_room_type_error != '' ) { echo '<li>' . $book_room_type_error . '</li>'; }
							if ( $book_date_from_error != '' ) { echo '<li>' . $book_date_from_error . '</li>'; }
							if ( $book_date_to_error != '' ) { echo '<li>' . $book_date_to_error . '</li>'; }
							if ( $book_full_name_error != '' ) { echo '<li>' . $book_full_name_error . '</li>'; }
							if ( $book_num_people_error != '' ) { echo '<li>' . $book_num_people_error . '</li>'; }
							if ( $book_email_error != '' ) { echo '<li>' . $book_email_error . '</li>'; }

							echo '</ul></div>';
							
						}
					
					?>
					
					<?php if ( $emailSent == true ) : ?>
						
						<table class="booking-table">
							<thead>
								<tr>
									<th><?php _e('Booking Option','qns'); ?></th>
									<th><?php _e('Selection','qns'); ?></th>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td><strong><?php _e('Room Type','qns'); ?>:</strong></td>
									<td><?php echo $book_room_type; ?></td>
								</tr>
								<tr>
									<td><strong><?php _e('Date','qns'); ?>:</strong></td>
									<td><?php echo $book_date_from; ?> - <?php echo $book_date_to; ?> (<?php echo $total_stay; ?> <?php _e('Nights','qns'); ?>)</td>
								</tr>
								<tr>
									<td><strong><?php _e('Name','qns'); ?>:</strong></td>
									<td><?php echo $book_full_name; ?></td>
								</tr>
								<tr>
									<td><strong><?php _e('Number of People','qns'); ?>:</strong></td>
									<td><?php echo $book_num_people; ?></td>
								</tr>
								<tr>
									<td><strong><?php _e('Email Address','qns'); ?>:</strong></td>
									<td><?php echo $book_email; ?></td>
								</tr>
								<tr>
									<td><strong><?php _e('Phone Number','qns'); ?>:</strong></td>
									<td><?php echo $book_phone; ?></td>
								</tr>
								<tr>
									<td><strong><?php _e('Special Requirements','qns'); ?>:</strong></td>
									<td><?php echo $book_message; ?></td>
								</tr>
								<tr class="table-highlight">
									<td><strong><?php _e('Total Cost','qns'); ?>:</strong></td>
									<td><?php echo $currency_unit.$total_price; ?></td>
								</tr>
							</tbody>
						</table>
					
					<?php else : ?>
					
					<!-- BEGIN .booknow-accompage -->
					<div class="booknow-accompage full-booking-form">
						
						<div class="book-price">
							
							<h2 class="price"><?php echo $currency_unit; ?><span class="room-price"><?php echo $total_price; ?></span><span class="price-detail"><span class="price-detail-value"><?php echo $total_stay; ?></span> <?php _e('Nights','qns'); ?></span></h2>
							
							<div class="price-tl"></div>
							<div class="price-tr"></div>
							<div class="price-bl"></div>
							<div class="price-br"></div>
							
						</div>
						
						<script>
							// Set room price for external JS file
							var getPrice = <?php echo $book_room_price; ?>;
						</script>
						
						<form class="booking-form booking-form-accompage" name="bookroom" action="<?php echo $booking_page; ?>" method="post">
							
							<input type="text" value="<?php echo $book_room_type; ?>" class="text-input" disabled="disabled">

							<div class="clearfix">
								<input type="text" name="book_date_from" id="datefrom" value="<?php echo $book_date_from; ?>" class="input-half datepicker">
								<input type="text" name="book_date_to" id="dateto" value="<?php echo $book_date_to; ?>" class="input-half input-half-last datepicker">
							</div>
							
							<input type="text" onblur="if(this.value=='')this.value='<?php _e('Full Name','qns'); ?>';" onfocus="if(this.value=='<?php _e('Full Name','qns'); ?>')this.value='';" value="<?php 
								if(isset($_POST['book_full_name'])) : echo $_POST['book_full_name']; 
								else : _e('Full Name','qns'); 
								endif;
							?>" name="book_full_name" class="text-input" />
							
							<input type="text" onblur="if(this.value=='')this.value='<?php _e('Number of People','qns'); ?>';" onfocus="if(this.value=='<?php _e('Number of People','qns'); ?>')this.value='';" value="<?php 
								if(isset($_POST['book_num_people'])) : echo $_POST['book_num_people']; 
								else : _e('Number of People','qns'); 
								endif;
							?>" name="book_num_people" class="text-input" />
							
							<input type="text" onblur="if(this.value=='')this.value='<?php _e('Email Address','qns'); ?>';" onfocus="if(this.value=='<?php _e('Email Address','qns'); ?>')this.value='';" value="<?php 
								if(isset($_POST['book_email'])) : echo $_POST['book_email']; 
								else : _e('Email Address','qns'); 
								endif;
							?>" name="book_email" class="text-input" />
							
							<input type="text" onblur="if(this.value=='')this.value='<?php _e('Phone Number','qns'); ?>';" onfocus="if(this.value=='<?php _e('Phone Number','qns'); ?>')this.value='';" value="<?php 
								if(isset($_POST['book_phone'])) : echo $_POST['book_phone']; 
								else : _e('Phone Number','qns'); 
								endif;
							?>" name="book_phone" class="text-input" />
							
							<textarea class="text-input" rows="6" name="book_message" onfocus="if(this.value=='<?php _e('Special Requirements','qns'); ?>')this.value='';" onblur="if(this.value=='')this.value='<?php _e('Special Requirements','qns'); ?>';"><?php 
								if(isset($_POST['book_message'])) : echo $_POST['book_message']; 
								endif;
							?></textarea>
							
							<input type="hidden" name="book_confirm" value="1" />
							<input type="hidden" name="book_room_type" value="<?php echo $book_room_type; ?>" />
							<input type="hidden" name="book_room_price" value="<?php echo $book_room_price; ?>" />
							<input type="hidden" name="submit" value="submit" />
							
							<input class="bookbutton" type="submit" value="<?php _e('Book Now','qns'); ?>" />

						</form>
						
					<!-- END .booknow-accompage -->
					</div>
					
					<?php endif; ?>
				
				<!-- END .one-half -->
				</div>
			
				<!-- BEGIN .one-half -->
				<div class="one-half last-col">
					
					<?php the_content(); ?>
				
				<!-- END .one-half -->
				</div>
				
			<!-- END .even-cols -->
			</div>
			
			<?php endif; ?>
			
		<!-- END .page-content -->
		</div>

	<!-- END .section -->
	</div>
	
<?php get_footer(); ?>

Is there a problem with you sending an email to the hotel, using the hotel’s own email address as the from-address? I wonder if your mail server is refusing to send it. If I’m reading it correctly, the first email you send is to the hotel, with both to- and from-address as $email_to, and the customers email address as the reply-to. But as you use the same $email_to as the from-address for the customer email (if I read correctly, that’s the second one you send) perhaps this isn’t the issue. It may well cause trouble later though.

Hmm, interesting… thanks soo much for teh response. Point out the snippet of code for the second email to me, just so I know I’m looking at the right one…

It’s the second place you call the qns_mail() function.

Aha, I see it, so you’re saying you think that changing that email may help, because possibly the Gmail smtp server may be refusing to send email to the same address that it’s from?.. and so, it’s worth a shot.

Hmm, but it’s coded in though…edit ideas?

Not because they are the same, but because the “from-address” you are using belongs to someone else. There are specific things you have to do on Gmail to be able to change the from-address: https://support.google.com/mail/answer/22370?hl=en which another post elsewhere suggests may no longer work. Allowing any old from-address would basically be “open relay” which is not good.

I’m not sure what you mean by that. It’s in the code, sure, but then so is everything else. Change the code so that it uses your own email to send the emails, with the reply-to box set to make it easier for both parties to reply if their mail client looks at that field.

Aha! I resolved the issue!

I did a few things…given your advice that it may be an email/SMTP issue, I did the following:
Used a domain email instead of the Gmail email as the receiving email (in theme options)
Used a different email address as the ‘reply to’ address (in theme options)
Forwarded the emails from that domain address to the Gmail (in email address settings)
Edited my SMTP settings (in theme options):
mail.mtplaisir.com
→ Port 465
→ SSL Encryption
—> SMTP Authentication turned on
→ SMTP Username and Password

…and it’s working now!

Thank you so much @droopsnoot for pointing me in the right direction. Awesomeness!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.