34462-vm/contact.php
Flatlogic Bot dcf4a840d2 1
2025-09-28 21:18:42 +00:00

48 lines
1.6 KiB
PHP

<?php
// /workspace/contact.php
header('Content-Type: application/json');
// Basic validation
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method Not Allowed']);
exit;
}
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$message = trim($_POST['message'] ?? '');
if (empty($name) || empty($email) || empty($message)) {
http_response_code(400);
echo json_encode(['error' => 'All fields are required.']);
exit;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
http_response_code(400);
echo json_encode(['error' => 'Invalid email format.']);
exit;
}
// Use MailService
require_once __DIR__ . '/mail/MailService.php';
// The recipient email address.
// IMPORTANT: For a real application, this should come from a config file or environment variable,
// not be hardcoded. Using the default from .env for this example.
$to = getenv('MAIL_TO') ?: null; // Let MailService use its default if not set
$subject = 'New Contact Form Submission from Coral Hub';
$res = MailService::sendContactMessage($name, $email, $message, $to, $subject);
if (!empty($res['success'])) {
echo json_encode(['success' => true, 'message' => 'Message sent successfully.']);
} else {
// Do not expose detailed mail errors to the client for security.
// Log this error on the server in a real application.
http_response_code(500);
error_log('MailService Error: ' . ($res['error'] ?? 'Unknown error'));
echo json_encode(['error' => 'Sorry, there was an issue sending your message. Please try again later.']);
}