34494-vm/contact.php
Flatlogic Bot 5acb599929 v2
2025-09-30 03:50:50 +00:00

42 lines
1.4 KiB
PHP

<?php
header('Content-Type: application/json');
// Allow only POST requests
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method Not Allowed']);
exit;
}
// Include the MailService
require_once __DIR__ . '/mail/MailService.php';
// Get and sanitize inputs
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$message = trim($_POST['message'] ?? '');
// Basic validation
if (empty($name) || empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL) || empty($message)) {
http_response_code(400);
echo json_encode(['error' => 'Please fill out all fields correctly.']);
exit;
}
// Prepare email details
$subject = 'New Contact Form Submission from Aperture Website';
// Send the email using the MailService.
// The `$to` address is omitted to use the default from the .env configuration.
$res = MailService::sendContactMessage($name, $email, $message, null, $subject);
// Respond to the client
if (!empty($res['success'])) {
echo json_encode(['success' => 'Thank you for your message! We will get back to you shortly.']);
} else {
http_response_code(500);
// Note: In a real app, you would log the detailed error.
// error_log('MailService Error: ' . ($res['error'] ?? 'Unknown error'));
echo json_encode(['error' => 'Sorry, there was an error sending your message. Please try again later.']);
}