35371-vm/contact.php
Flatlogic Bot 74ec2be88b Ali Khan
2025-10-31 02:29:37 +00:00

39 lines
1.3 KiB
PHP

<?php
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(['success' => false, 'error' => 'Invalid request method.']);
exit;
}
require_once __DIR__ . '/mail/MailService.php';
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$message = trim($_POST['message'] ?? '');
if (empty($name) || empty($email) || empty($message)) {
echo json_encode(['success' => false, 'error' => 'Please fill out all fields.']);
exit;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo json_encode(['success' => false, 'error' => 'Invalid email format.']);
exit;
}
// The recipient email address. You can change this to a configurable variable later.
// If left null, it will use the MAIL_TO from the .env file.
$to = null;
$subject = 'New Contact Form Message from Jalwa Game';
$res = MailService::sendContactMessage($name, $email, $message, $to, $subject);
if (!empty($res['success'])) {
echo json_encode(['success' => true]);
} else {
// Avoid exposing detailed mail server errors to the client.
error_log('MailService Error: ' . ($res['error'] ?? 'Unknown error'));
echo json_encode(['success' => false, 'error' => 'Failed to send message. Please try again later.']);
}