Search code examples
phpsqlphpmailer

How to send an email accepting or rejecting a request for a reservation


My goal is to have the ability to accept or reject a request from a customer through email. What I have is the back office of my website, where it appears everyone that submitted to my reservation form, the customer after submitting receives an email saying that their confirmation is pending and at that moment is when the admin has the option to accept or reject the reservation, after that action is done, the customer would receive an email with the response. Right now, I tried to do the "accept" button just to see if it would work. Right now, i get an error message saying that there's an invalid Address $mail->addAddress($_POST['EmailReser']);

//File listreser.php

<body style="background-color:white;">
<?php
    	$sql="select * from Reservas";
	$res=$lig->query($sql);
?>

<div class="container">
  <h1 align="center">List Reservations</h1> <br><br>      
  <table id ="tableUser"  class="table table-striped display">
    <thead>
	<tr>
    <th>Code of the reservation</th>
    <th>Name </th>
    <th>Last Name</th>
		<th>Email</th>
		<th>Telephone</th>
    <th>Date </th>
		<th>Time</th>
    <th>Number of people</th>
		<th>Message </th>
		<th>State</th>
		<th></th>
		<th></th>
      </tr>
    </thead>
    <tbody>
<?php 
while ($lin=$res->fetch_array()){ ?>
      <tr>
        <td><?php echo$lin[Cod_Reserva]; ?></td>
		<td><?php echo$lin[NomeReser]; ?></td>
		<td><?php echo$lin[ApelidoReser]; ?></td>
		<td><?php echo$lin[EmailReser]; ?></td>
		<td><?php echo$lin[TelefoneR]; ?></td>
		<td><?php echo$lin[DataR]; ?></td>
		<td><?php echo$lin[Hora]; ?></td>
		<td><?php echo$lin[NumPessoas]; ?></td>
		<td><?php echo$lin[MensagemR]; ?></td>     
 <td><a style="padding-right:12px;" href=index.php?cmd=aceitar&Cod_Reserva=<?php echo $lin["Cod_Reserva"]; ?>><img src='images/check.png' width="20" height = "20" ></a><a href=index.php?cmd=rejeitar&Cod_Reserva=<?php echo $lin["Cod_Reserva"]; ?>><img src='images/close.png' width="15" height = "15" ></td>

      </tr>
 <?php 
}
 ?>
  </tbody>
  </table>
</div>

<?php
 var_dump($_GET);
 exit();
 // $Cod_Reserva=$_REQUEST['Cod_Reserva'];  
 $Cod_Reserva=$_REQUEST['Cod_Reserva'];
	//$EmailReser = $_GET['EmailReser'];
   // $sql="select * from Reservas ";
    $sql="select EmailReser from reservas where cod_reserva = $Cod_Reserva";
	$res=$lig->query($sql);
	//$lin=$res->fetch_array();
	 while ($lin=$res->fetch_array()){ 
	require 'PHPMailer/PHPMailerAutoload.php';
	$mail = new PHPMailer;
	$mail->CharSet = "utf-8";
	$mail->Host='smtp.gmail.com';
	$mail->Port = 587;
	$mail->SMTPDebug = 2;
	$mail->SMTPAuth = true;
	$mail->SMTPSecure='tls';
	$mail->Username='[email protected]';
	$mail->Password='****';
	$mail->setFrom('[email protected]');
	$mail->addAddress($lin['EmailReser']);
	$mail->addReplyTo('[email protected]');
	
	$mail->isHTML(true);
	$mail->Subject='Your request has been confirmed';
	$mail->Body = "message";
	
	if(!$mail->send()){
		echo "<script>
                alert('There's been a mistake');
              </script>";
	}
	else{		
		 echo "<script>
                alert('Email was sent with success');

               </script>";
}
 
 }

?>


Solution

  • You could replace your button with a link to a separate PHP script that sends the acceptance email. You'd need to include enough information to know which reservation you're accepting (and hence where to send it), and you'd need to add some validation to ensure that only suitable users can actually process the acceptance.

    So something like just the end of your code

    <a href='acceptance.php?ref=<?php echo $lin["Cod Reserva"]; ?>'>Accept</a>
    

    to call that script and pass in that value as $_GET['ref'], for each row in your table. Sorry I don't know if that's the correct field to pass in, but it needs to be the unique ID for the reservation. Your acceptance.php script then retrieves the rest of the information from the database to get the email address to use.

    Or you could draw a html form in each row, stick the reference in a hidden variable, draw a button, and have that call the separate script.

    Sending the email in a separate script instead of the same one makes it easy to add Ajax later on for users that have JS enabled.