45 lines
1.9 KiB
PHP
45 lines
1.9 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
require_once __DIR__ . '/db/config.php';
|
|
require_once __DIR__ . '/mail/MailService.php';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$name = trim(filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING));
|
|
$email = trim(filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL));
|
|
$message = trim(filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING));
|
|
|
|
if ($name && $email && $message) {
|
|
try {
|
|
$pdo = db();
|
|
$sql = "INSERT INTO contact_submissions (name, email, message) VALUES (?, ?, ?)";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([$name, $email, $message]);
|
|
|
|
// Send email notification
|
|
$to = getenv('MAIL_TO') ?: 'support@sunsills.com'; // Fallback recipient
|
|
$subject = "New Contact Form Submission from {$name}";
|
|
$emailBody = "<p>You have received a new message from your website contact form.</p>";
|
|
$emailBody .= "<p><strong>Name:</strong> {$name}</p>";
|
|
$emailBody .= "<p><strong>Email:</strong> {$email}</p>";
|
|
$emailBody .= "<p><strong>Message:</strong><br>{$message}</p>";
|
|
|
|
MailService::sendMail($to, $subject, $emailBody, strip_tags($emailBody), ['reply_to' => $email]);
|
|
|
|
$_SESSION['success_message'] = "Thank you for your message! We will get back to you shortly.";
|
|
} catch (PDOException $e) {
|
|
// In a real app, you would log this error
|
|
$_SESSION['error_message'] = "Sorry, there was an error processing your request. Please try again later.";
|
|
} catch (Exception $e) {
|
|
$_SESSION['error_message'] = "Could not send email. Please try again later.";
|
|
}
|
|
} else {
|
|
$_SESSION['error_message'] = "Invalid input. Please fill out all fields correctly.";
|
|
}
|
|
} else {
|
|
$_SESSION['error_message'] = "Invalid request method.";
|
|
}
|
|
|
|
header("Location: index.php#contact");
|
|
exit();
|