43 lines
1.4 KiB
PHP
43 lines
1.4 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
// Include MailService
|
|
require_once __DIR__ . '/mail/MailService.php';
|
|
|
|
// Get form data
|
|
$name = filter_var(trim($_POST["name"]), FILTER_SANITIZE_STRING);
|
|
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
|
|
$message = filter_var(trim($_POST["message"]), FILTER_SANITIZE_STRING);
|
|
|
|
// Validate form data
|
|
if (empty($name) || empty($email) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
// Set error message and redirect
|
|
$_SESSION['contact_form_status'] = 'error';
|
|
header("Location: index.php#contact");
|
|
exit;
|
|
}
|
|
|
|
// Send email
|
|
$to = getenv('MAIL_TO') ?: 'admin@example.com'; // Fallback email
|
|
$subject = "New Contact Form Submission from " . $name;
|
|
|
|
$res = MailService::sendContactMessage($name, $email, $message, $to, $subject);
|
|
|
|
if (!empty($res['success'])) {
|
|
// Set success message and redirect
|
|
$_SESSION['contact_form_status'] = 'success';
|
|
} else {
|
|
// Set error message and redirect
|
|
$_SESSION['contact_form_status'] = 'error';
|
|
// Optionally log the error: error_log($res['error']);
|
|
}
|
|
header("Location: index.php#contact");
|
|
exit;
|
|
|
|
} else {
|
|
// If not a POST request, redirect to home
|
|
header("Location: index.php");
|
|
exit;
|
|
}
|