mail sending issue solved

This commit is contained in:
Dharmaraj Shrestha 2025-02-21 15:37:20 +05:45
parent f3e88e83a9
commit 07aa3e1643
2 changed files with 262 additions and 82 deletions

View File

@ -9,6 +9,12 @@
# RewriteCond %{THE_REQUEST} \s/+(.*?)\.php[\s?] [NC]
# RewriteRule ^ /%1 [R=301,L,NE]
<Files "mailBooking.php">
RewriteEngine Off
</Files>
<Files "mailCContact.php">
RewriteEngine Off
</Files>
RewriteEngine On
DirectoryIndex index.php

View File

@ -1,99 +1,273 @@
<!-- <?php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
// Start session to store messages
session_start();
#you are required to change these credential only
$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';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
var_dump($_SERVER['REQUEST_METHOD']);
exit;
// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Collect form data
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$subject = htmlspecialchars($_POST['subject']);
$phone = htmlspecialchars($_POST['phone']);
$message = htmlspecialchars($_POST['message']);
// 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';
// Gmail credentials
$gmailUser = 'subashgiri939@gmail.com'; // Replace with your Gmail address
$gmailPassword = 'tbub jgpc jmwp iznl'; // Replace with your Gmail app password
// Check if the URL already has query parameters
$separator = (strpos($redirect, '?') !== false) ? '&' : '?';
// Create a new PHPMailer instance
$mail = new PHPMailer(true);
// 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 {
// Server settings
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = $gmailUser;
$mail->Password = $gmailPassword;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
/*** User Confirmation Email ***/
$userMail = new PHPMailer(true);
// Send email to the customer (thank you message)
$mail->setFrom($gmailUser, 'Bohora Car Rental');
$mail->addAddress($email, $name); // Send to the customer's email address
$mail->isHTML(true);
$mail->Subject = 'Thanks for Contacting Us!';
$mail->Body = "
<h3>Thank you for contacting Bohora Car Rental:</h3>
<p>Dear $name,</p>
<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!
";
// 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;
$mail->send();
// User Email Configuration
$userMail->setFrom($sending_email, $support_name);
$userMail->addAddress($user_email);
$userMail->isHTML(true);
$userMail->Subject = "Thank you for contacting ". $support_name;
// Send email to your Gmail (admin's email)
$mail->clearAddresses(); // Clear the previous recipient (customer)
$mail->addAddress($gmailUser, 'Bohora Car Rental'); // Send to your company email
// Embed logo
$userMail->addEmbeddedImage('img/logo/logo-trans.png', 'logo_cid');
$mail->Subject = 'New Contact Request';
$mail->Body = "
<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.
";
// 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>
$mail->send();
</div>
</body>
</html>
HTML;
// Redirect to a thank you page or your contact page
/*** 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) {
// Redirect to an error page if something goes wrong
// 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();
}
} 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;
}
}
?>