File "sendEmail.php"

Full Path: /home/chawlag2/public_html/cars/admin/vendor/fontawesome-free/svgs/sendEmail.php
File size: 6.44 KB
MIME-type: text/x-php
Charset: utf-8

<?php
// sendEmail.php

if ($_SERVER["REQUEST_METHOD"] !== "POST") {
  http_response_code(405);
  exit("Method Not Allowed");
}

// ---------- Helper ----------
function clean($val) {
  return trim((string)$val);
}
function esc($val) {
  return htmlspecialchars((string)$val, ENT_QUOTES, 'UTF-8');
}

// ---------- Collect + sanitize ----------
$name    = clean($_POST['name'] ?? '');
$surname = clean($_POST['surname'] ?? '');
$company = clean($_POST['company'] ?? '');
$userEmail = clean($_POST['email'] ?? '');
$phone   = clean($_POST['phone'] ?? '');
$city    = clean($_POST['city'] ?? '');
$color   = clean($_POST['color'] ?? '');
$message = clean($_POST['message'] ?? '');

// optional hidden fields (if present)
$brand = clean($_POST['brand'] ?? 'DONGFENG');
$model = clean($_POST['model'] ?? 'CGM-007');

// ---------- Basic validation ----------
$errors = [];

if ($name === '') $errors[] = "Name is required";
if ($surname === '') $errors[] = "Surname is required";
if ($phone === '') $errors[] = "Phone is required";
if ($city === '') $errors[] = "City is required";
if ($color === '') $errors[] = "Exterior color is required";
if ($message === '') $errors[] = "Message is required";

if ($userEmail === '' || !filter_var($userEmail, FILTER_VALIDATE_EMAIL)) {
  $errors[] = "A valid email is required";
}

if (!empty($errors)) {
  // You can customize this error behavior
  echo "<script>alert('Please correct the following:\\n- " . esc(implode("\\n- ", $errors)) . "');history.back();</script>";
  exit;
}

// pretty formatting
$fullName = mb_convert_case($name . ' ' . $surname, MB_CASE_TITLE, "UTF-8");
$companyDisplay = ($company !== '') ? mb_convert_case($company, MB_CASE_TITLE, "UTF-8") : "-";
$cityDisplay = mb_convert_case($city, MB_CASE_TITLE, "UTF-8");
$colorDisplay = esc($color);
$messageHtml = nl2br(esc($message));

// ---------- Admin email ----------
$adminTo = [
  "info@chawlagreenmotors.com",
  // add more admin recipients if you want:
  // "syedzeeshanali38@gmail.com"
];

$adminSubject = "New Booking Request Received! ($brand - $model)";

// Use a domain email as FROM for better deliverability
$fromEmail = "info@chawlagreenmotors.com";
$fromName  = "Chawla Green Motors";

// headers for admin (HTML)
$adminHeaders  = "MIME-Version: 1.0\r\n";
$adminHeaders .= "Content-type:text/html;charset=UTF-8\r\n";
$adminHeaders .= "From: " . $fromName . " <" . $fromEmail . ">\r\n";
$adminHeaders .= "Reply-To: " . esc($fullName) . " <" . $userEmail . ">\r\n";
$adminHeaders .= "X-Mailer: PHP/" . phpversion() . "\r\n";

// logo absolute URL
$logoUrl = "https://chawlagreenmotors.com/images/logo2.png";

$adminMsg = "
<html>
<head><meta charset='UTF-8'></head>
<body style='font-family: Arial, sans-serif;'>
  <div style='max-width: 720px; margin: 0 auto;'>
    <div style='text-align:center; padding: 10px 0;'>
      <img src='{$logoUrl}' width='140' alt='Chawla Green Motors' style='max-width:140px;'>
    </div>

    <h2 style='margin: 10px 0; color:#111;'>New booking/request received</h2>
    <p style='margin: 0 0 14px; color:#333;'>
      Dear Sir,<br>
      A new booking request has been submitted with the details below:
    </p>

    <table cellpadding='8' cellspacing='0' border='1' style='border-collapse: collapse; width:100%;'>
      <tr><td><b>Name</b></td><td>" . esc($fullName) . "</td></tr>
      <tr><td><b>Email</b></td><td>" . esc($userEmail) . "</td></tr>
      <tr><td><b>Contact No</b></td><td>" . esc($phone) . "</td></tr>
      <tr><td><b>City</b></td><td>" . esc($cityDisplay) . "</td></tr>
      <tr><td><b>Company</b></td><td>" . esc($companyDisplay) . "</td></tr>
      <tr><td><b>Exterior Color</b></td><td>{$colorDisplay}</td></tr>
      <tr><td><b>Brand</b></td><td>" . esc($brand) . "</td></tr>
      <tr><td><b>Model</b></td><td>" . esc($model) . "</td></tr>
      <tr><td><b>Message</b></td><td>{$messageHtml}</td></tr>
    </table>

    <p style='margin-top: 18px; color:#333;'>
      Regards,<br>
      <b>Chawla Green Motors</b>
    </p>

    <div style='margin-top: 8px;'>
      <img src='{$logoUrl}' width='100' alt='Chawla Green Motors' style='max-width:100px;'>
    </div>
  </div>
</body>
</html>
";

// send admin email to all recipients
$adminSent = true;
foreach ($adminTo as $recipient) {
  if (!mail($recipient, $adminSubject, $adminMsg, $adminHeaders)) {
    $adminSent = false;
  }
}

// ---------- Confirmation email to user ----------
$userSubject = "We received your request - Chawla Green Motors";

$userHeaders  = "MIME-Version: 1.0\r\n";
$userHeaders .= "Content-type:text/html;charset=UTF-8\r\n";
$userHeaders .= "From: " . $fromName . " <" . $fromEmail . ">\r\n";
$userHeaders .= "Reply-To: " . $fromName . " <" . $fromEmail . ">\r\n";
$userHeaders .= "X-Mailer: PHP/" . phpversion() . "\r\n";

$userMsg = "
<html>
<head><meta charset='UTF-8'></head>
<body style='font-family: Arial, sans-serif;'>
  <div style='max-width: 720px; margin: 0 auto;'>
    <div style='text-align:center; padding: 10px 0;'>
      <img src='{$logoUrl}' width='140' alt='Chawla Green Motors' style='max-width:140px;'>
    </div>

    <h2 style='margin: 10px 0; color:#111;'>Thank you for contacting us</h2>

    <p style='color:#333; line-height: 1.6;'>
      Dear " . esc($fullName) . ",<br><br>
      We have received your request successfully. Our team will contact you shortly.<br><br>
      <b>Request details:</b><br>
      Brand: " . esc($brand) . "<br>
      Model: " . esc($model) . "<br>
      Exterior Color: {$colorDisplay}<br>
      City: " . esc($cityDisplay) . "<br>
    </p>

    <p style='color:#333;'>
      Regards,<br>
      <b>Chawla Green Motors</b><br>
      Phone: +92 311 4995162<br>
      Email: info@chawlagreenmotors.com
    </p>
  </div>
</body>
</html>
";

$userSent = mail($userEmail, $userSubject, $userMsg, $userHeaders);

// ---------- Final response ----------
if ($adminSent) {
  // Even if user confirmation fails, we can still show success
  echo "<script>alert('Thank you! Your request has been received. " . ($userSent ? "A confirmation email has been sent to your email address." : "We could not send the confirmation email at the moment.") . "');</script>";
  echo "<script>window.location.href='index.html';</script>";
  exit;
} else {
  echo "<script>alert('Sorry! We could not submit your request right now. Please try again later.');</script>";
  echo "<script>history.back();</script>";
  exit;
}
?>