PATH:
home
/
u865795251
/
domains
/
whatisnewis.com
/
public_html
/
everestentp
/
Editing: mail-handler.php
<?php /** * Everest Enterprise - Mail Handler * Handles form submissions and sends styled HTML emails * to both admin and the submitting user. */ require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . '/db.php'; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; session_start(); // ============================================================ // CONFIGURATION — Loaded from .env fallback // ============================================================ $ADMIN_EMAIL = getSetting('admin_email', $_ENV['ADMIN_EMAIL'] ?? 'amandeepdigitalhandlersindia@gmail.com'); $FROM_EMAIL = getSetting('from_email', $_ENV['FROM_EMAIL'] ?? 'amandeepdigitalhandlersindia@gmail.com'); $FROM_NAME = getSetting('from_name', $_ENV['FROM_NAME'] ?? 'Everest Enterprise'); $PHONE_NUMBER = getSetting('call_number', $_ENV['PHONE_NUMBER'] ?? '+91 9167762534'); $WEBSITE_URL = getSetting('website_url', $_ENV['WEBSITE_URL'] ?? 'https://everestentp.com/'); header('Content-Type: application/json; charset=utf-8'); header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: POST, OPTIONS'); header('Access-Control-Allow-Headers: Content-Type'); if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(200); exit; } if ($_SERVER['REQUEST_METHOD'] !== 'POST') { echo json_encode(['success' => false, 'message' => 'Invalid request method.']); exit; } // ============================================================ // CSRF Protection Check // ============================================================ $submittedToken = $_POST['csrf_token'] ?? ''; if (empty($submittedToken) || !isset($_SESSION['csrf_token']) || $submittedToken !== $_SESSION['csrf_token']) { echo json_encode(['success' => false, 'message' => 'Security token invalid. Please reload the page and try again.']); exit; } // ============================================================ // Collect POST data // ============================================================ $name = sanitize($_POST['name'] ?? ''); $email = sanitize($_POST['email'] ?? ''); $phone = sanitize($_POST['phone'] ?? ''); $budget = sanitize($_POST['budget'] ?? ''); $quantity = sanitize($_POST['quantity'] ?? ''); $specs = sanitize($_POST['specs'] ?? ''); $message = sanitize($_POST['message'] ?? ''); $category = sanitize($_POST['category'] ?? ''); $company = sanitize($_POST['company'] ?? ''); $form_type = sanitize($_POST['form_type'] ?? 'general'); // ============================================================ // Validation // ============================================================ if (empty($name) || empty($email)) { echo json_encode(['success' => false, 'message' => 'Name and Email are required.']); exit; } if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { echo json_encode(['success' => false, 'message' => 'Please provide a valid email address.']); exit; } // ============================================================ // Determine form label // ============================================================ $form_labels = [ 'hero' => 'Hero Section Inquiry', 'inquiry' => 'Floating Button Inquiry', 'cta' => 'CTA Section Inquiry', 'category' => 'Category Inquiry – ' . $category, 'general' => 'General Inquiry', ]; $form_label = $form_labels[$form_type] ?? 'Website Inquiry'; // ============================================================ // Build detail rows for the email // ============================================================ $details = []; $details[] = ['label' => 'Name', 'value' => $name]; $details[] = ['label' => 'Email', 'value' => $email]; if ($phone) $details[] = ['label' => 'Phone', 'value' => $phone]; if ($company) $details[] = ['label' => 'Company', 'value' => $company]; if ($budget) $details[] = ['label' => 'Budget', 'value' => $budget]; if ($quantity) $details[] = ['label' => 'Quantity', 'value' => $quantity]; if ($specs) $details[] = ['label' => 'Specifications', 'value' => $specs]; if ($category) $details[] = ['label' => 'Category', 'value' => $category]; if ($message) $details[] = ['label' => 'Message', 'value' => nl2br(htmlspecialchars($message))]; // ============================================================ // 0. Save to Database // ============================================================ try { $pdo = getDB(); $stmt = $pdo->prepare("INSERT INTO inquiries (name, email, phone, company, budget, quantity, specs, message, category, form_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$name, $email, $phone, $company, $budget, $quantity, $specs, $message, $category, $form_type]); } catch (Exception $e) { error_log("Database Insert Fail in mail-handler.php: " . $e->getMessage()); } // ============================================================ // 1. Send Admin Email // ============================================================ $adminSubject = "New Inquiry: {$FROM_NAME} from {$name}"; $adminBody = buildAdminEmail($details, $form_label, $name, $email, $WEBSITE_URL, $PHONE_NUMBER); $adminSent = sendEmail($ADMIN_EMAIL, $adminSubject, $adminBody); // ============================================================ // 2. Send User Thank-You Email // ============================================================ $userSubject = "Thank You for Your Inquiry Everest Enterprise"; $userBody = buildUserEmail($name, $WEBSITE_URL, $PHONE_NUMBER, $ADMIN_EMAIL); $userSent = sendEmail($email, $userSubject, $userBody, true); // ============================================================ // Response // ============================================================ if ($adminSent === true) { echo json_encode([ 'success' => true, 'message' => 'Thank you! Your inquiry has been submitted successfully. We\'ll get back to you shortly.' ]); } else { // If it failed, show the specific PHPMailer error temporarily for debugging $errorMsg = is_string($adminSent) ? $adminSent : 'There was a problem sending your inquiry. Please try again or call us directly.'; echo json_encode([ 'success' => false, 'message' => 'Email Error: ' . $errorMsg ]); } // ============================================================ // HELPER FUNCTIONS // ============================================================ function sanitize($str) { return htmlspecialchars(strip_tags(trim($str)), ENT_QUOTES, 'UTF-8'); } function sendEmail($to, $subject, $htmlBody, $isUserNotification = false) { $smtpHost = getSetting('smtp_host', $_ENV['SMTP_HOST'] ?? ''); $fromEmail = getSetting('from_email', $_ENV['FROM_EMAIL'] ?? 'amandeepdigitalhandlersindia@gmail.com'); $fromName = getSetting('from_name', $_ENV['FROM_NAME'] ?? 'Everest Enterprise'); $adminEmail = getSetting('admin_email', $_ENV['ADMIN_EMAIL'] ?? 'amandeepdigitalhandlersindia@gmail.com'); if (empty($smtpHost)) { // Fallback to mail() if SMTP isn't configured in .env yet $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=UTF-8\r\n"; $headers .= "From: " . $fromName . " <" . $fromEmail . ">\r\n"; $headers .= "Reply-To: " . ($isUserNotification ? $adminEmail : $fromEmail) . "\r\n"; return mail($to, $subject, $htmlBody, $headers); } $mail = new PHPMailer(true); try { // Capture raw SMTP debug transcript to help identify the exact authentication block $GLOBALS['smtp_debug'] = ''; $mail->SMTPDebug = 3; $mail->Debugoutput = function($str, $level) { $GLOBALS['smtp_debug'] .= $str . " | "; }; $mail->isSMTP(); $mail->Host = $smtpHost; $mail->SMTPAuth = true; $mail->Username = getSetting('smtp_user', $_ENV['SMTP_USER'] ?? ''); $mail->Password = getSetting('smtp_pass', $_ENV['SMTP_PASS'] ?? ''); $mail->SMTPSecure = getSetting('smtp_secure', $_ENV['SMTP_SECURE'] ?? 'tls'); $mail->Port = getSetting('smtp_port', $_ENV['SMTP_PORT'] ?? 587); $mail->setFrom($fromEmail, $fromName); $mail->addAddress($to); if ($isUserNotification) { $mail->addReplyTo($adminEmail); } else { $mail->addReplyTo($fromEmail); } $mail->isHTML(true); $mail->Subject = $subject; $mail->Body = $htmlBody; return $mail->send(); } catch (Exception $e) { $exactError = $mail->ErrorInfo ?: $e->getMessage(); $debugInfo = strip_tags($GLOBALS['smtp_debug']); error_log("PHPMailer Error: " . $exactError . " | " . $debugInfo); // Return exactly what the Gmail server said in the transcript return $exactError . ". Server Response: " . substr($debugInfo, strpos($debugInfo, 'SERVER -> CLIENT: 535')); } } // ============================================================ // EMAIL TEMPLATE: ADMIN NOTIFICATION // ============================================================ function buildAdminEmail($details, $formLabel, $name, $email, $websiteUrl, $phone) { $rows = ''; foreach ($details as $d) { $rows .= ' <tr> <td style="padding: 12px 16px; font-weight: 600; color: #1a3012; background: #f9fdf6; border-bottom: 1px solid #eef5e8; width: 140px; font-size: 14px; vertical-align: top;"> ' . $d['label'] . ' </td> <td style="padding: 12px 16px; color: #333; border-bottom: 1px solid #f0f0f0; font-size: 14px; vertical-align: top;"> ' . $d['value'] . ' </td> </tr>'; } $date = date('d M Y, h:i A'); return ' <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="margin: 0; padding: 0; background-color: #f4f4f4; font-family: \'Segoe UI\', Tahoma, Geneva, Verdana, sans-serif;"> <table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color: #f4f4f4;"> <tr> <td align="center" style="padding: 30px 15px;"> <table role="presentation" width="600" border="0" cellspacing="0" cellpadding="0" style="max-width: 600px; width: 100%;"> <!-- Header --> <tr> <td style="background: linear-gradient(135deg, #1a3012 0%, #2a4a1d 100%); padding: 30px 40px; border-radius: 12px 12px 0 0; text-align: center;"> <h1 style="margin: 0 0 8px; font-size: 26px; color: #ffffff; font-weight: 700; letter-spacing: 0.5px;"> Everest<span style="color: #d11618;">Enterprise</span> </h1> <p style="margin: 0; color: rgba(255,255,255,0.7); font-size: 13px; letter-spacing: 1px; text-transform: uppercase;"> New Inquiry Received </p> </td> </tr> <!-- Alert Bar --> <tr> <td style="background: #d11618; padding: 14px 40px; text-align: center;"> <p style="margin: 0; color: #fff; font-size: 14px; font-weight: 600;"> 🔔 ' . $formLabel . ' • ' . $date . ' </p> </td> </tr> <!-- Body --> <tr> <td style="background: #ffffff; padding: 35px 40px;"> <p style="margin: 0 0 20px; font-size: 16px; color: #333; line-height: 1.6;"> You have received a new inquiry from <strong style="color: #1a3012;">' . htmlspecialchars($name) . '</strong>. Here are the details: </p> <!-- Details Table --> <table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0" style="border: 1px solid #e8e8e8; border-radius: 10px; overflow: hidden; margin-bottom: 25px;"> ' . $rows . ' </table> <!-- Quick Actions --> <table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="center" style="padding: 10px 0;"> <a href="mailto:' . htmlspecialchars($email) . '" style="display: inline-block; background: linear-gradient(135deg, #d11618, #a11112); color: #fff; padding: 12px 35px; border-radius: 8px; text-decoration: none; font-weight: 600; font-size: 14px; letter-spacing: 0.5px;"> ✉️ Reply to ' . htmlspecialchars($name) . ' </a> </td> </tr> </table> </td> </tr> <!-- Footer --> <tr> <td style="background: #fafafa; padding: 25px 40px; border-radius: 0 0 12px 12px; border-top: 1px solid #eee; text-align: center;"> <p style="margin: 0 0 5px; color: #999; font-size: 12px;"> This email was sent from your website contact form. </p> <p style="margin: 0; color: #bbb; font-size: 11px;"> ' . htmlspecialchars($websiteUrl) . ' </p> </td> </tr> </table> </td> </tr> </table> </body> </html>'; } // ============================================================ // EMAIL TEMPLATE: USER THANK YOU // ============================================================ function buildUserEmail($name, $websiteUrl, $phone, $adminEmail) { return ' <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="margin: 0; padding: 0; background-color: #f4f4f4; font-family: \'Segoe UI\', Tahoma, Geneva, Verdana, sans-serif;"> <table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color: #f4f4f4;"> <tr> <td align="center" style="padding: 30px 15px;"> <table role="presentation" width="600" border="0" cellspacing="0" cellpadding="0" style="max-width: 600px; width: 100%;"> <!-- Header with logo --> <tr> <td style="background: linear-gradient(135deg, #1a3012 0%, #2a4a1d 100%); padding: 35px 40px; border-radius: 12px 12px 0 0; text-align: center;"> <h1 style="margin: 0 0 6px; font-size: 28px; color: #ffffff; font-weight: 700; letter-spacing: 0.5px;"> Everest<span style="color: #d11618;">Enterprise</span> </h1> <p style="margin: 0; color: rgba(255,255,255,0.6); font-size: 12px; letter-spacing: 2px; text-transform: uppercase;"> Premium Corporate Gifting </p> </td> </tr> <!-- Green accent bar --> <tr> <td style="background: linear-gradient(135deg, #65b03e, #4a8a2d); padding: 4px 0;"></td> </tr> <!-- Body Content --> <tr> <td style="background: #ffffff; padding: 40px;"> <!-- Thank You Heading --> <div style="text-align: center; margin-bottom: 30px;"> <div style="display: inline-block; width: 60px; height: 60px; background: linear-gradient(135deg, #65b03e, #4a8a2d); border-radius: 50%; line-height: 60px; text-align: center; margin-bottom: 15px;"> <span style="font-size: 28px;">✓</span> </div> <h2 style="margin: 0 0 5px; font-size: 24px; color: #1a3012; font-weight: 700;"> Thank You, ' . htmlspecialchars($name) . '! </h2> <p style="margin: 0; color: #65b03e; font-size: 14px; font-weight: 600;"> Your inquiry has been received successfully </p> </div> <!-- Message --> <p style="margin: 0 0 20px; font-size: 15px; color: #555; line-height: 1.8; text-align: center;"> We appreciate your interest in our corporate gifting solutions. Our team will review your requirements and get back to you with <strong style="color: #d11618;">personalized gift options with prices</strong> in an easily shareable PDF. </p> <!-- What Happens Next Box --> <table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0" style="margin: 25px 0;"> <tr> <td style="background: #f9fdf6; border: 1px solid #e8f5e0; border-radius: 10px; padding: 25px 30px;"> <h3 style="margin: 0 0 15px; font-size: 16px; color: #1a3012; font-weight: 700;"> ✨ What happens next? </h3> <table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td style="padding: 6px 0; vertical-align: top; width: 30px;"> <span style="display: inline-block; width: 22px; height: 22px; background: #65b03e; color: #fff; border-radius: 50%; text-align: center; line-height: 22px; font-size: 12px; font-weight: bold;">1</span> </td> <td style="padding: 6px 0 6px 10px; color: #555; font-size: 14px; line-height: 1.5;"> Our gifting expert will review your requirements </td> </tr> <tr> <td style="padding: 6px 0; vertical-align: top; width: 30px;"> <span style="display: inline-block; width: 22px; height: 22px; background: #65b03e; color: #fff; border-radius: 50%; text-align: center; line-height: 22px; font-size: 12px; font-weight: bold;">2</span> </td> <td style="padding: 6px 0 6px 10px; color: #555; font-size: 14px; line-height: 1.5;"> You\'ll receive curated gift options in a shareable PDF </td> </tr> <tr> <td style="padding: 6px 0; vertical-align: top; width: 30px;"> <span style="display: inline-block; width: 22px; height: 22px; background: #65b03e; color: #fff; border-radius: 50%; text-align: center; line-height: 22px; font-size: 12px; font-weight: bold;">3</span> </td> <td style="padding: 6px 0 6px 10px; color: #555; font-size: 14px; line-height: 1.5;"> Shortlist products & request free physical samples </td> </tr> <tr> <td style="padding: 6px 0; vertical-align: top; width: 30px;"> <span style="display: inline-block; width: 22px; height: 22px; background: #65b03e; color: #fff; border-radius: 50%; text-align: center; line-height: 22px; font-size: 12px; font-weight: bold;">4</span> </td> <td style="padding: 6px 0 6px 10px; color: #555; font-size: 14px; line-height: 1.5;"> We handle logo customization & deliver anywhere in India (even globally!) </td> </tr> </table> </td> </tr> </table> <!-- Highlight Stats --> <table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0" style="margin: 20px 0;"> <tr> <td width="33%" style="text-align: center; padding: 15px 5px;"> <div style="font-size: 24px; font-weight: 800; color: #d11618;">4000+</div> <div style="font-size: 12px; color: #888; margin-top: 4px;">Products</div> </td> <td width="33%" style="text-align: center; padding: 15px 5px; border-left: 1px solid #eee; border-right: 1px solid #eee;"> <div style="font-size: 24px; font-weight: 800; color: #d11618;">450+</div> <div style="font-size: 12px; color: #888; margin-top: 4px;">Corporate Clients</div> </td> <td width="33%" style="text-align: center; padding: 15px 5px;"> <div style="font-size: 24px; font-weight: 800; color: #d11618;">100K+</div> <div style="font-size: 12px; color: #888; margin-top: 4px;">Products Delivered</div> </td> </tr> </table> <!-- Urgent CTA --> <table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0" style="margin-top: 25px;"> <tr> <td style="background: linear-gradient(135deg, #fef2f2, #fff5f5); border: 1px solid #fde8e8; border-radius: 10px; padding: 20px 25px; text-align: center;"> <p style="margin: 0 0 6px; font-size: 12px; color: #d11618; text-transform: uppercase; letter-spacing: 1.5px; font-weight: 600;"> Super Urgent? </p> <p style="margin: 0 0 12px; font-size: 15px; color: #333; font-weight: 500;"> Get in touch with us immediately </p> <a href="tel:' . str_replace(' ', '', $phone) . '" style="display: inline-block; background: linear-gradient(135deg, #65b03e, #4a8a2d); color: #fff; padding: 10px 30px; border-radius: 8px; text-decoration: none; font-weight: 600; font-size: 14px;"> 📞 Call Us: ' . htmlspecialchars($phone) . ' </a> </td> </tr> </table> </td> </tr> <!-- Footer --> <tr> <td style="background: #1a3012; padding: 30px 40px; border-radius: 0 0 12px 12px; text-align: center;"> <p style="margin: 0 0 8px; color: #fff; font-size: 15px; font-weight: 600;"> Everest<span style="color: #d11618;">Enterprise</span> </p> <p style="margin: 0 0 15px; color: rgba(255,255,255,0.5); font-size: 12px;"> Premium Corporate Gifting Solutions </p> <table role="presentation" border="0" cellspacing="0" cellpadding="0" align="center"> <tr> <td style="padding: 0 8px;"> <a href="tel:' . str_replace(' ', '', $phone) . '" style="color: rgba(255,255,255,0.7); font-size: 12px; text-decoration: none;">📞 ' . htmlspecialchars($phone) . '</a> </td> <td style="color: rgba(255,255,255,0.3);">|</td> <td style="padding: 0 8px;"> <a href="mailto:' . htmlspecialchars($adminEmail) . '" style="color: rgba(255,255,255,0.7); font-size: 12px; text-decoration: none;">✉️ ' . htmlspecialchars($adminEmail) . '</a> </td> </tr> </table> <p style="margin: 20px 0 0; color: rgba(255,255,255,0.3); font-size: 11px;"> © ' . date('Y') . ' Everest Enterprise. All rights reserved. </p> </td> </tr> </table> </td> </tr> </table> </body> </html>'; } ?>
SAVE
CANCEL