41 lines
1.4 KiB
PHP
41 lines
1.4 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
require_once __DIR__ . '/mail/MailService.php';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$name = trim($_POST['name'] ?? '');
|
|
$email = trim($_POST['email'] ?? '');
|
|
$message = trim($_POST['message'] ?? '');
|
|
|
|
if (empty($name) || empty($email) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$_SESSION['contact_form_status'] = 'error';
|
|
$_SESSION['contact_form_message'] = 'Please fill out all fields with valid information.';
|
|
header('Location: /#contact');
|
|
exit;
|
|
}
|
|
|
|
// The user did not specify a recipient, so we'll use the default from the .env
|
|
// and let the MailService handle the fallback.
|
|
$to = getenv('MAIL_TO') ?: null;
|
|
$subject = 'New Contact Form Submission from ' . $name;
|
|
|
|
$res = MailService::sendContactMessage($name, $email, $message, $to, $subject);
|
|
|
|
if (!empty($res['success'])) {
|
|
$_SESSION['contact_form_status'] = 'success';
|
|
$_SESSION['contact_form_message'] = 'Thank you! Your message has been sent successfully.';
|
|
} else {
|
|
error_log('MailService Error: ' . ($res['error'] ?? 'Unknown error'));
|
|
$_SESSION['contact_form_status'] = 'error';
|
|
$_SESSION['contact_form_message'] = 'Sorry, there was an error sending your message. Please try again later.';
|
|
}
|
|
|
|
header('Location: /#contact');
|
|
exit;
|
|
}
|
|
|
|
// Redirect home if accessed directly
|
|
header('Location: /');
|
|
exit;
|