43 lines
1.2 KiB
PHP
43 lines
1.2 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
// Get form data
|
|
$name = strip_tags(trim($_POST["name"]));
|
|
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
|
|
$message = strip_tags(trim($_POST["message"]));
|
|
|
|
// Validate form data
|
|
if (empty($name) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
// Redirect back to the form with an error message
|
|
header("Location: index.php?status=error#contact");
|
|
exit;
|
|
}
|
|
|
|
// Include the MailService
|
|
require_once __DIR__ . '/mail/MailService.php';
|
|
|
|
// Set the recipient email address from environment variables
|
|
$to = getenv('MAIL_TO') ?: getenv('MAIL_FROM');
|
|
|
|
|
|
// Set the subject
|
|
$subject = "New contact from $name";
|
|
|
|
// Send the email
|
|
$result = MailService::sendContactMessage($name, $email, $message, $to, $subject);
|
|
|
|
if ($result['success']) {
|
|
// Redirect to a success page
|
|
header("Location: contact_success.php");
|
|
} else {
|
|
// Redirect back to the form with an error message
|
|
header("Location: contact_error.php");
|
|
}
|
|
|
|
} else {
|
|
// If not a POST request, redirect to the homepage
|
|
header("Location: index.php");
|
|
exit;
|
|
}
|
|
?>
|