41 lines
1.5 KiB
PHP
41 lines
1.5 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
require_once __DIR__ . '/mail/MailService.php';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$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);
|
|
|
|
if (empty($name) || empty($email) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$_SESSION['status'] = 'error';
|
|
$_SESSION['message'] = 'Please fill out all fields correctly.';
|
|
header("Location: index.php#contact");
|
|
exit;
|
|
}
|
|
|
|
// The recipient email address.
|
|
// IMPORTANT: Replace this with your real email address.
|
|
$to = getenv('MAIL_TO') ?: 'your-email@example.com';
|
|
|
|
$res = MailService::sendContactMessage($name, $email, $message, $to, 'Contact Form Submission');
|
|
|
|
if (!empty($res['success'])) {
|
|
$_SESSION['status'] = 'success';
|
|
$_SESSION['message'] = 'Thank you for your message! We will get back to you shortly.';
|
|
} else {
|
|
$_SESSION['status'] = 'error';
|
|
$_SESSION['message'] = 'Sorry, there was an error sending your message. Please try again later.';
|
|
// It's a good practice to log the actual error for debugging
|
|
// error_log("MailService Error: " . $res['error']);
|
|
}
|
|
|
|
header("Location: index.php#contact");
|
|
exit;
|
|
} else {
|
|
// Not a POST request, redirect to the form
|
|
header("Location: index.php#contact");
|
|
exit;
|
|
}
|