37032-vm/contact_handler.php
2025-12-18 07:14:58 +00:00

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['status'] = 'error';
$_SESSION['message'] = 'Please fill out all fields correctly.';
header('Location: index.php#contact');
exit;
}
// The recipient email address. Should be configured in .env as MAIL_TO
$to = getenv('MAIL_TO') ?: 'info@tpspetroleums.com';
$subject = 'New Contact Form Submission from ' . $name;
$res = MailService::sendContactMessage($name, $email, $message, $to, $subject);
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 good practice to log the actual error for debugging, but not show it to the user.
error_log('MailService Error: ' . ($res['error'] ?? 'Unknown error'));
}
header('Location: index.php#contact');
exit;
} else {
// If not a POST request, redirect home.
header('Location: index.php');
exit;
}