38 lines
1.3 KiB
PHP
38 lines
1.3 KiB
PHP
<?php
|
|
// Basic security: check if it's a POST request
|
|
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
|
|
header("Location: index.php?status=error&message=Invalid request method.");
|
|
exit;
|
|
}
|
|
|
|
require_once __DIR__ . '/mail/MailService.php';
|
|
|
|
// Sanitize and validate inputs
|
|
$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) || !filter_var($email, FILTER_VALIDATE_EMAIL) || empty($message)) {
|
|
header("Location: index.php?status=error&message=Please fill out all fields correctly.");
|
|
exit;
|
|
}
|
|
|
|
// Use MailService to send the email
|
|
// The recipient is determined by the MAIL_TO environment variable in .env
|
|
// The user's email is set as the Reply-To address.
|
|
$res = MailService::sendContactMessage(
|
|
$name,
|
|
$email,
|
|
$message,
|
|
null, // Use default recipient from config
|
|
'New Contact Form Submission'
|
|
);
|
|
|
|
if (!empty($res['success'])) {
|
|
header("Location: index.php?status=success&message=Your message has been sent successfully!");
|
|
exit;
|
|
} else {
|
|
// In a real app, you would log the detailed error: error_log('MailService Error: ' . $res['error']);
|
|
header("Location: index.php?status=error&message=Sorry, there was an error sending your message.");
|
|
exit;
|
|
} |