264 lines
9.0 KiB
PHP
264 lines
9.0 KiB
PHP
<?php include('mailCredentials.php'); ?>
|
|
<?php
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
require 'vendor/autoload.php';
|
|
|
|
// Enable error reporting
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 1);
|
|
|
|
// A helper function to build the redirect URL with a status parameter
|
|
function buildRedirectUrl($status, $extra = '')
|
|
{
|
|
// If HTTP_REFERER isn't set, use a default page
|
|
$redirect = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'car-grid.php';
|
|
|
|
// Check if the URL already has query parameters
|
|
$separator = (strpos($redirect, '?') !== false) ? '&' : '?';
|
|
|
|
// Build and return the new URL
|
|
return $redirect . $separator . "status=" . $status . $extra;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
// Get form data and sanitize inputs
|
|
$name = htmlspecialchars(trim($_POST['name']));
|
|
$car_name = htmlspecialchars(trim($_POST['car_name']));
|
|
$location = htmlspecialchars(trim($_POST['location']));
|
|
$pickup_date = htmlspecialchars(trim($_POST['pickup_date']));
|
|
$dropoff_date = htmlspecialchars(trim($_POST['dropoff_date']));
|
|
$user_email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL);
|
|
$phone = htmlspecialchars(trim($_POST['phone']));
|
|
$subject = $car_name . " Booking";
|
|
|
|
// Check if all required fields are provided
|
|
if (
|
|
!empty($name) && !empty($user_email)
|
|
&& !empty($location) && !empty($pickup_date) && !empty($dropoff_date) && !empty($phone)
|
|
) {
|
|
try {
|
|
/*** User Confirmation Email ***/
|
|
$userMail = new PHPMailer(true);
|
|
|
|
// SMTP Configuration
|
|
$userMail->isSMTP();
|
|
$userMail->Host = 'smtp.gmail.com';
|
|
$userMail->SMTPAuth = true;
|
|
$userMail->Username = $sending_email;
|
|
$userMail->Password = $app_password;
|
|
$userMail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
|
$userMail->Port = 587;
|
|
|
|
// User Email Configuration
|
|
$userMail->setFrom($sending_email, $support_name);
|
|
$userMail->addAddress($user_email);
|
|
$userMail->isHTML(true);
|
|
$userMail->Subject = "Thank you for contacting ". $support_name;
|
|
|
|
// Embed logo
|
|
$userMail->addEmbeddedImage('img/logo/logo-trans.png', 'logo_cid');
|
|
|
|
// User Email Template (Responsive & clean design)
|
|
$userMail->Body = <<<HTML
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Booking Confirmation</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<style>
|
|
.email-container {
|
|
max-width: 600px;
|
|
margin: 0 auto;
|
|
font-family: Arial, sans-serif;
|
|
padding: 1rem;
|
|
}
|
|
.header {
|
|
text-align: center;
|
|
padding-bottom: 1rem;
|
|
}
|
|
.details {
|
|
background: #f8f9fa;
|
|
padding: 1.5rem;
|
|
border-radius: 8px;
|
|
margin: 1rem 0;
|
|
}
|
|
.footer {
|
|
text-align: center;
|
|
font-size: 0.8rem;
|
|
color: #666;
|
|
margin-top: 1rem;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="email-container">
|
|
<div class="header">
|
|
<img src="cid:logo_cid" alt="Company Logo" style="max-height: 80px;">
|
|
<h2 class="mt-3">Booking Confirmation</h2>
|
|
</div>
|
|
<p>Dear {$name},</p>
|
|
<p>Thank you for reaching out to $support_name. We have received your booking request. Below are your booking details:</p>
|
|
<div class="details">
|
|
<h4>Booking Details</h4>
|
|
<p><strong>Car Name:</strong> {$car_name}</p>
|
|
<p><strong>Drop-off Location:</strong> {$location}</p>
|
|
<p><strong>Pickup Date:</strong> {$pickup_date}</p>
|
|
<p><strong>Drop-off Date:</strong> {$dropoff_date}</p>
|
|
</div>
|
|
<p>Our team will contact you shortly to confirm your booking and provide further details.</p>
|
|
<p>If you have any questions, please reply to this email.</p>
|
|
<p>Best regards,<br>$support_name Team</p>
|
|
<div class="footer">
|
|
<p>{$company_name} | {$company_address}</p>
|
|
<p>Email: {$company_email} | Phone: {$company_phone}</p>
|
|
</div>
|
|
<p class="mb-0"><em>If you didn't initiate this booking, please ignore this email. This may have been sent in error.</em></p>
|
|
|
|
</div>
|
|
</body>
|
|
</html>
|
|
HTML;
|
|
|
|
/*** Admin Notification Email ***/
|
|
$adminMail = new PHPMailer(true);
|
|
|
|
// SMTP Configuration
|
|
$adminMail->isSMTP();
|
|
$adminMail->Host = 'smtp.gmail.com';
|
|
$adminMail->SMTPAuth = true;
|
|
$adminMail->Username = $sending_email;
|
|
$adminMail->Password = $app_password;
|
|
$adminMail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
|
$adminMail->Port = 587;
|
|
|
|
$adminMail->setFrom($sending_email, $support_name);
|
|
$adminMail->addAddress($company_email);
|
|
$adminMail->addReplyTo($user_email, $name);
|
|
$adminMail->isHTML(true);
|
|
$adminMail->Subject = "New Booking Request: {$subject}";
|
|
|
|
// Embed logo for admin email
|
|
$adminMail->addEmbeddedImage('img/logo/logo-trans.png', 'logo_cid');
|
|
|
|
$currentDateTime = date('Y-m-d H:i:s');
|
|
|
|
// Admin Email Template (all details nicely presented)
|
|
$adminMail->Body = <<<HTML
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>New Booking Request</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<style>
|
|
.email-container {
|
|
max-width: 800px;
|
|
margin: 0 auto;
|
|
font-family: Arial, sans-serif;
|
|
padding: 1rem;
|
|
}
|
|
.header {
|
|
text-align: center;
|
|
padding-bottom: 1rem;
|
|
}
|
|
.details {
|
|
background: #f8f9fa;
|
|
padding: 1.5rem;
|
|
border-radius: 8px;
|
|
margin: 1rem 0;
|
|
}
|
|
.details h4 {
|
|
margin-bottom: 1rem;
|
|
}
|
|
.details p {
|
|
margin: 0.5rem 0;
|
|
}
|
|
.footer {
|
|
text-align: center;
|
|
font-size: 0.8rem;
|
|
color: #666;
|
|
margin-top: 1rem;
|
|
}
|
|
.btn-reply {
|
|
margin-top: 1rem;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="email-container">
|
|
<div class="header">
|
|
<img src="cid:logo_cid" alt="Company Logo" style="max-height: 80px;">
|
|
<h2 class="mt-3">New Booking Request Received</h2>
|
|
</div>
|
|
<div class="details">
|
|
<h4>Booking Information</h4>
|
|
<p><strong>Full Name:</strong> {$name}</p>
|
|
<p><strong>Email:</strong> {$user_email}</p>
|
|
<p><strong>Phone Number:</strong> {$phone}</p>
|
|
<p><strong>Car Name:</strong> {$car_name}</p>
|
|
<p><strong>Drop-off Location:</strong> {$location}</p>
|
|
<p><strong>Pickup Date:</strong> {$pickup_date}</p>
|
|
<p><strong>Drop-off Date:</strong> {$dropoff_date}</p>
|
|
<p><strong>Subject:</strong> {$subject}</p>
|
|
<p><strong>Message:</strong><br>{$message}</p>
|
|
</div>
|
|
<div class="text-center">
|
|
<a href="mailto:{$user_email}?subject=Re: {$subject}" class="btn btn-primary btn-reply">Reply to {$name}</a>
|
|
</div>
|
|
<div class="footer">
|
|
<p>{$company_name} | {$company_address}</p>
|
|
<p>Email: {$company_email} | Phone: {$company_phone}</p>
|
|
<p>Received on: {$currentDateTime}</p>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
HTML;
|
|
|
|
// Send the emails
|
|
$userMail->send();
|
|
$adminMail->send();
|
|
|
|
// Redirect back to the referring page with a success status
|
|
$redirectUrl = buildRedirectUrl('success');
|
|
header("Location: $redirectUrl");
|
|
exit();
|
|
|
|
} catch (Exception $e) {
|
|
// On error, encode the message and redirect back with the error status
|
|
$errorMessage = "&message=" . urlencode($e->getMessage());
|
|
$redirectUrl = buildRedirectUrl('error', $errorMessage);
|
|
header("Location: $redirectUrl");
|
|
exit();
|
|
}
|
|
|
|
} else {
|
|
// Missing fields: redirect back with missing_fields status
|
|
$redirectUrl = buildRedirectUrl('missing_fields');
|
|
header("Location: $redirectUrl");
|
|
exit();
|
|
}
|
|
}
|
|
|
|
// Include header after potential redirects
|
|
include('header.php');
|
|
|
|
// Handle status messages (this part is optional if you want to display messages on the referring page)
|
|
if (isset($_GET['status'])) {
|
|
switch ($_GET['status']) {
|
|
case 'success':
|
|
echo "<p class='alert alert-success'>✅ Your message has been sent successfully!</p>";
|
|
break;
|
|
case 'error':
|
|
$error = isset($_GET['message']) ? htmlspecialchars($_GET['message']) : "There was an error sending your message. Please try again.";
|
|
echo "<p class='alert alert-danger'>❌ $error</p>";
|
|
break;
|
|
case 'missing_fields':
|
|
echo "<p class='alert alert-warning'>⚠️ Please fill in all the required fields.</p>";
|
|
break;
|
|
}
|
|
}
|
|
?>
|