36705-vm/contact.php
Flatlogic Bot 426700b053 2.0 Handy
2025-12-06 09:31:42 +00:00

39 lines
1.4 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/mail/MailService.php';
// Basic validation
if (empty($_POST['name']) || empty($_POST['email'])) {
echo json_encode(['success' => false, 'message' => 'Name and Email are required.']);
exit;
}
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
echo json_encode(['success' => false, 'message' => 'Invalid email format.']);
exit;
}
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message'] ?? 'No message provided.');
// The email body that the admin will receive
$fullMessage = "New training interest sign-up:\n\nName: {$name}\nEmail: {$email}\n\nMessage:\n{$message}";
// Send email to admin/organizer. The recipient is configured in .env (MAIL_TO)
$result = MailService::sendContactMessage(
$name, // User's name
$email, // User's email, used as Reply-To
$fullMessage // The message body
// $to is omitted, so it falls back to MAIL_TO from config
);
if (!empty($result['success'])) {
echo json_encode(['success' => true, 'message' => 'Thank you for your interest! We will be in touch shortly.']);
} else {
// In a real app, log the detailed error: error_log('MailService Error: ' . $result['error']);
echo json_encode(['success' => false, 'message' => 'Sorry, there was an issue sending your message. Please try again later.']);
}