<?php header("Content-Type: application/json"); // Enable error reporting for debugging ini_set("display_errors", 1); ini_set("display_startup_errors", 1); error_reporting(E_ALL); // Log errors to a file ini_set("log_errors", 1); ini_set("error_log", __DIR__ . "/php-error.log"); // 1. Configuration $receiving_email = "info@k2bc.com"; $subject_prefix = "[K2BC Booking Request] "; $allowed_methods = ["POST"]; // 2. Security and Validation if (!in_array($_SERVER["REQUEST_METHOD"], $allowed_methods)) { http_response_code(405); echo json_encode(["success" => false, "message" => "Method Not Allowed"]); exit; } // Check if data is present if (empty($_POST)) { http_response_code(400); echo json_encode(["success" => false, "message" => "No data received."]); exit; } // Function to sanitize input function sanitize_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } // 3. Extract and Sanitize Form Data $trekName = sanitize_input($_POST["trekName"] ?? "N/A"); $name = sanitize_input($_POST["name"] ?? ""); $email = sanitize_input($_POST["email"] ?? ""); $phone = sanitize_input($_POST["phone"] ?? "N/A"); $participants = sanitize_input($_POST["participants"] ?? "N/A"); $departureDate = sanitize_input($_POST["departureDate"] ?? "N/A"); $experience = sanitize_input($_POST["experience"] ?? "N/A"); $requests = sanitize_input($_POST["requests"] ?? "None"); // Basic validation for required fields if (empty($name) || empty($email) || empty($departureDate)) { http_response_code(400); echo json_encode(["success" => false, "message" => "Required fields (Name, Email, Departure Date) are missing."]); error_log("Booking form validation failed: Missing required fields."); exit; } // Validate email format if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { http_response_code(400); echo json_encode(["success" => false, "message" => "Invalid email format."]); error_log("Booking form validation failed: Invalid email format for: " . $email); exit; } // 4. Construct Email Content $email_subject = $subject_prefix . $trekName . " - " . $name; $email_body = "A new trek booking request has been submitted via the K2BC website.\n\n"; $email_body .= "--- Booking Details ---\n"; $email_body .= "Trek Name: " . $trekName . "\n"; $email_body .= "Departure Date: " . $departureDate . "\n"; $email_body .= "Participants: " . $participants . "\n"; $email_body .= "Experience Level: " . ucfirst($experience) . "\n\n"; $email_body .= "--- Contact Information ---\n"; $email_body .= "Full Name: " . $name . "\n"; $email_body .= "Email: " . $email . "\n"; $email_body .= "Phone: " . $phone . "\n\n"; $email_body .= "--- Special Requests ---\n"; $email_body .= $requests . "\n\n"; $email_body .= "--------------------------\n"; $email_body .= "Please contact the client as soon as possible to confirm the booking."; // 5. Set Email Headers $headers = "From: K2BC Website <no-reply@k2bc.com>\r\n"; $headers .= "Reply-To: " . $email . "\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/plain; charset=UTF-8\r\n"; // 6. Send Email require_once __DIR__ . "/send_email_smtp.php"; if (send_smtp_email($receiving_email, $email_subject, $email_body, $email, "K2BC Booking Request")) { // Success response http_response_code(200); echo json_encode(["success" => true, "message" => "Booking request sent successfully!"]); error_log("Booking email sent successfully to " . $receiving_email); } else { // Failure response http_response_code(500); $error_message = "Failed to send email via SMTP. Check php-error.log for details."; echo json_encode(["success" => false, "message" => $error_message]); error_log("Booking email sending failed: " . $error_message); } ?>