35044-vm/contact.php
Flatlogic Bot 3aad7fe5c1 1.0
2025-10-18 13:11:52 +00:00

43 lines
1.6 KiB
PHP

<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Basic server-side validation
if (empty($_POST['name']) || empty($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) || empty($_POST['message'])) {
$_SESSION['status'] = 'error';
$_SESSION['message'] = 'Please fill out all fields correctly.';
header('Location: index.php#contact');
exit;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email = strip_tags(htmlspecialchars($_POST['email']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// Load MailService
require_once __DIR__ . '/mail/MailService.php';
// The recipient email address should be configured in your .env file (MAIL_TO)
// or you can specify one here. For this example, we let it use the default.
$to = null;
$subject = "New Contact Form Submission from " . $name;
$res = MailService::sendContactMessage($name, $email, $message, $to, $subject);
if (!empty($res['success'])) {
$_SESSION['status'] = 'success';
$_SESSION['message'] = 'Thank you for your message! We will get back to you shortly.';
} else {
// In a real app, you would log the detailed error: $res['error']
$_SESSION['status'] = 'error';
$_SESSION['message'] = 'Sorry, there was an error sending your message. Please try again later.';
}
} else {
// Not a POST request
$_SESSION['status'] = 'error';
$_SESSION['message'] = 'Invalid request method.';
}
header('Location: index.php#contact');
exit;