34935-vm/contact.php
2025-10-13 16:57:50 +00:00

35 lines
1.2 KiB
PHP

<?php
header('Content-Type: application/json');
// Basic validation
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) {
echo json_encode(['success' => false, 'message' => 'Please fill out all fields.']);
exit;
}
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
echo json_encode(['success' => false, 'message' => 'Invalid email format.']);
exit;
}
require_once __DIR__ . '/mail/MailService.php';
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
// The recipient email address should be configured in .env as MAIL_TO
// If MAIL_TO is not set, it will fall back to MAIL_FROM
$to = null;
$subject = 'New Contact Form Submission from Worldsphere.ai';
$res = MailService::sendContactMessage($name, $email, $message, $to, $subject);
if (!empty($res['success'])) {
echo json_encode(['success' => true, 'message' => 'Thank you for your message! We will get back to you shortly.']);
} else {
// In a real app, you would log the detailed error.
// error_log("Mailgun error: " . $res['error']);
echo json_encode(['success' => false, 'message' => 'Sorry, there was an error sending your message. Please try again later.']);
}