mail sending issue solved
This commit is contained in:
parent
f3e88e83a9
commit
07aa3e1643
@ -9,6 +9,12 @@
|
|||||||
# RewriteCond %{THE_REQUEST} \s/+(.*?)\.php[\s?] [NC]
|
# RewriteCond %{THE_REQUEST} \s/+(.*?)\.php[\s?] [NC]
|
||||||
# RewriteRule ^ /%1 [R=301,L,NE]
|
# RewriteRule ^ /%1 [R=301,L,NE]
|
||||||
|
|
||||||
|
<Files "mailBooking.php">
|
||||||
|
RewriteEngine Off
|
||||||
|
</Files>
|
||||||
|
<Files "mailCContact.php">
|
||||||
|
RewriteEngine Off
|
||||||
|
</Files>
|
||||||
|
|
||||||
RewriteEngine On
|
RewriteEngine On
|
||||||
DirectoryIndex index.php
|
DirectoryIndex index.php
|
||||||
|
336
optional.php
336
optional.php
@ -1,99 +1,273 @@
|
|||||||
<!-- <?php
|
<?php
|
||||||
use PHPMailer\PHPMailer\PHPMailer;
|
use PHPMailer\PHPMailer\PHPMailer;
|
||||||
use PHPMailer\PHPMailer\Exception;
|
use PHPMailer\PHPMailer\Exception;
|
||||||
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
// Start session to store messages
|
#you are required to change these credential only
|
||||||
session_start();
|
$company_name= 'Bohara Rental Team';
|
||||||
|
$support_name= 'Bohara Rental Support';
|
||||||
|
$company_address = 'Kathmandu, Nepal';
|
||||||
|
$company_email = 'subedigokul119@gmail.com';
|
||||||
|
$company_phone = +9779800000000;
|
||||||
|
$sending_email = 'noreply.notification.developer@gmail.com';
|
||||||
|
$app_password= 'uuky rjvm expm notx';
|
||||||
|
|
||||||
require 'src/Exception.php';
|
var_dump($_SERVER['REQUEST_METHOD']);
|
||||||
require 'src/PHPMailer.php';
|
exit;
|
||||||
require 'src/SMTP.php';
|
// Enable error reporting
|
||||||
|
error_reporting(E_ALL);
|
||||||
|
ini_set('display_errors', 1);
|
||||||
|
|
||||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
// A helper function to build the redirect URL with a status parameter
|
||||||
// Collect form data
|
function buildRedirectUrl($status, $extra = '')
|
||||||
$name = htmlspecialchars($_POST['name']);
|
{
|
||||||
$email = htmlspecialchars($_POST['email']);
|
// If HTTP_REFERER isn't set, use a default page
|
||||||
$subject = htmlspecialchars($_POST['subject']);
|
$redirect = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'car-grid.php';
|
||||||
$phone = htmlspecialchars($_POST['phone']);
|
|
||||||
$message = htmlspecialchars($_POST['message']);
|
|
||||||
|
|
||||||
// Gmail credentials
|
// Check if the URL already has query parameters
|
||||||
$gmailUser = 'subashgiri939@gmail.com'; // Replace with your Gmail address
|
$separator = (strpos($redirect, '?') !== false) ? '&' : '?';
|
||||||
$gmailPassword = 'tbub jgpc jmwp iznl'; // Replace with your Gmail app password
|
|
||||||
|
|
||||||
// Create a new PHPMailer instance
|
// Build and return the new URL
|
||||||
$mail = new PHPMailer(true);
|
return $redirect . $separator . "status=" . $status . $extra;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
||||||
// Server settings
|
// Get form data and sanitize inputs
|
||||||
$mail->isSMTP();
|
$name = htmlspecialchars(trim($_POST['name']));
|
||||||
$mail->Host = 'smtp.gmail.com';
|
$car_name = htmlspecialchars(trim($_POST['car_name']));
|
||||||
$mail->SMTPAuth = true;
|
$location = htmlspecialchars(trim($_POST['location']));
|
||||||
$mail->Username = $gmailUser;
|
$pickup_date = htmlspecialchars(trim($_POST['pickup_date']));
|
||||||
$mail->Password = $gmailPassword;
|
$dropoff_date = htmlspecialchars(trim($_POST['dropoff_date']));
|
||||||
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
|
$user_email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL);
|
||||||
$mail->Port = 465;
|
$phone = htmlspecialchars(trim($_POST['phone']));
|
||||||
|
$subject = $car_name . " Booking";
|
||||||
|
|
||||||
// Send email to the customer (thank you message)
|
// Check if all required fields are provided
|
||||||
$mail->setFrom($gmailUser, 'Bohora Car Rental');
|
if (
|
||||||
$mail->addAddress($email, $name); // Send to the customer's email address
|
!empty($name) && !empty($user_email)
|
||||||
$mail->isHTML(true);
|
&& !empty($location) && !empty($pickup_date) && !empty($dropoff_date) && !empty($phone)
|
||||||
$mail->Subject = 'Thanks for Contacting Us!';
|
) {
|
||||||
$mail->Body = "
|
try {
|
||||||
<h3>Thank you for contacting Bohora Car Rental:</h3>
|
/*** User Confirmation Email ***/
|
||||||
<p>Dear $name,</p>
|
$userMail = new PHPMailer(true);
|
||||||
<p>We have received your contact form submission, and our team will get back to you as soon as possible.</p>
|
|
||||||
<p><strong>Subject:</strong> $subject</p>
|
|
||||||
<p><strong>Message:</strong> $message</p>
|
|
||||||
<p><strong>Phone:</strong> $phone</p>
|
|
||||||
<p>Thank you for reaching out!</p>
|
|
||||||
";
|
|
||||||
$mail->AltBody = "
|
|
||||||
Thank you for contacting Bohora Car Rental:
|
|
||||||
Dear $name,
|
|
||||||
We have received your contact form submission, and our team will get back to you as soon as possible.
|
|
||||||
Subject: $subject
|
|
||||||
Message: $message
|
|
||||||
Phone: $phone
|
|
||||||
Thank you for reaching out!
|
|
||||||
";
|
|
||||||
|
|
||||||
$mail->send();
|
// 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;
|
||||||
|
|
||||||
// Send email to your Gmail (admin's email)
|
// User Email Configuration
|
||||||
$mail->clearAddresses(); // Clear the previous recipient (customer)
|
$userMail->setFrom($sending_email, $support_name);
|
||||||
$mail->addAddress($gmailUser, 'Bohora Car Rental'); // Send to your company email
|
$userMail->addAddress($user_email);
|
||||||
|
$userMail->isHTML(true);
|
||||||
|
$userMail->Subject = "Thank you for contacting ". $support_name;
|
||||||
|
|
||||||
$mail->Subject = 'New Contact Request';
|
// Embed logo
|
||||||
$mail->Body = "
|
$userMail->addEmbeddedImage('img/logo/logo-trans.png', 'logo_cid');
|
||||||
<h3>You have received a new contact request:</h3>
|
|
||||||
<p><strong>Name:</strong> $name</p>
|
|
||||||
<p><strong>Email:</strong> $email</p>
|
|
||||||
<p><strong>Phone:</strong> $phone</p>
|
|
||||||
<p><strong>Subject:</strong> $subject</p>
|
|
||||||
<p><strong>Message:</strong> $message</p>
|
|
||||||
<p>Please review the details for further action.</p>
|
|
||||||
";
|
|
||||||
$mail->AltBody = "
|
|
||||||
You have received a new contact request:
|
|
||||||
Name: $name
|
|
||||||
Email: $email
|
|
||||||
Phone: $phone
|
|
||||||
Subject: $subject
|
|
||||||
Message: $message
|
|
||||||
Please review the details for further action.
|
|
||||||
";
|
|
||||||
|
|
||||||
$mail->send();
|
// 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>
|
||||||
|
|
||||||
// Redirect to a thank you page or your contact page
|
</div>
|
||||||
exit();
|
</body>
|
||||||
|
</html>
|
||||||
|
HTML;
|
||||||
|
|
||||||
} catch (Exception $e) {
|
/*** Admin Notification Email ***/
|
||||||
// Redirect to an error page if something goes wrong
|
$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();
|
exit();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
echo 'Invalid request method.';
|
|
||||||
}
|
}
|
||||||
?> -->
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
Loading…
x
Reference in New Issue
Block a user