false, 'message' => 'Method Not Allowed']); exit; } $full_name = trim($_POST['full_name'] ?? ''); $email = trim($_POST['email'] ?? ''); $phone = trim($_POST['phone'] ?? null); $subject = trim($_POST['subject'] ?? ''); $message = trim($_POST['message'] ?? ''); $latitude = !empty($_POST['latitude']) ? trim($_POST['latitude']) : null; $longitude = !empty($_POST['longitude']) ? trim($_POST['longitude']) : null; if (empty($full_name) || empty($email) || empty($subject) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'Please fill in all required fields and provide a valid email.']); exit; } try { $db = db(); $stmt = $db->prepare( "INSERT INTO support_tickets (full_name, email, phone, subject, message, latitude, longitude) VALUES (?, ?, ?, ?, ?, ?, ?)" ); $stmt->execute([$full_name, $email, $phone, $subject, $message, $latitude, $longitude]); // Notify support team $support_email_to = 'support@majuroeats.com'; $support_email_subject = "New Support Ticket: " . htmlspecialchars($subject); $support_email_html = "
Name: " . htmlspecialchars($full_name) . "
Email: " . htmlspecialchars($email) . "
" . ($phone ? "Phone: " . htmlspecialchars($phone) . "
" : "") . "Subject: " . htmlspecialchars($subject) . "
Message:
" . nl2br(htmlspecialchars($message)) . "
" . ($latitude && $longitude ? "Location: View on Map
" : "") . " "; $support_email_text = strip_tags($support_email_html); MailService::sendMail($support_email_to, $support_email_subject, $support_email_html, $support_email_text, ['reply_to' => $email]); // Send confirmation to user $user_email_subject = "We've received your message | MajuroEats Support"; $user_email_html = "Hi " . htmlspecialchars($full_name) . ",
We've received your support request and a member of our team will get back to you shortly. Here is a copy of your message:
Subject: " . htmlspecialchars($subject) . "
Message:
" . nl2br(htmlspecialchars($message)) . "
With thanks,
The MajuroEats Team
"; $user_email_text = strip_tags($user_email_html); MailService::sendMail($email, $user_email_subject, $user_email_html, $user_email_text); echo json_encode(['success' => true, 'message' => 'Thank you! Our support team will contact you soon.']); } catch (Exception $e) { http_response_code(500); // In a real app, you would log this error, not expose it to the user. echo json_encode(['success' => false, 'message' => 'An unexpected error occurred. Please try again later.', 'error' => $e->getMessage()]); }