108 lines
3.7 KiB
PHP
108 lines
3.7 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
// Load dependencies
|
|
require_once __DIR__ . '/db/config.php';
|
|
require_once __DIR__ . '/mail/MailService.php';
|
|
|
|
// --- Database Schema Setup ---
|
|
try {
|
|
$pdo = db();
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS contact_submissions (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
name VARCHAR(255) NOT NULL,
|
|
email VARCHAR(255) NOT NULL,
|
|
subject VARCHAR(255) NOT NULL,
|
|
message TEXT NOT NULL,
|
|
submitted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)");
|
|
} catch (PDOException $e) {
|
|
error_log("Database schema creation failed: " . $e->getMessage());
|
|
echo json_encode(['success' => false, 'error' => 'Could not initialize database. Please try again later.']);
|
|
exit;
|
|
}
|
|
|
|
// --- Form Submission Handling ---
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
echo json_encode(['success' => false, 'error' => 'Invalid request method.']);
|
|
exit;
|
|
}
|
|
|
|
// --- Input Validation ---
|
|
$name = trim($_POST['name'] ?? '');
|
|
$email = trim($_POST['email'] ?? '');
|
|
$subject = trim($_POST['subject'] ?? '');
|
|
$message = trim($_POST['message'] ?? '');
|
|
|
|
if (empty($name) || empty($email) || empty($subject) || empty($message)) {
|
|
echo json_encode(['success' => false, 'error' => 'Please fill out all fields.']);
|
|
exit;
|
|
}
|
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
echo json_encode(['success' => false, 'error' => 'Please provide a valid email address.']);
|
|
exit;
|
|
}
|
|
|
|
// --- Store in Database ---
|
|
try {
|
|
$stmt = $pdo->prepare("INSERT INTO contact_submissions (name, email, subject, message) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$name, $email, $subject, $message]);
|
|
} catch (PDOException $e) {
|
|
error_log("Database insert failed: " . $e->getMessage());
|
|
echo json_encode(['success' => false, 'error' => 'Could not save your message. Please try again later.']);
|
|
exit;
|
|
}
|
|
|
|
// --- Send Email Notifications ---
|
|
$siteOwnerEmail = getenv('MAIL_TO') ?: (getenv('MAIL_FROM') ?: 'admin@example.com');
|
|
$projectName = $_SERVER['PROJECT_NAME'] ?? 'Your Website';
|
|
|
|
// 1. Notification to Site Owner
|
|
$ownerSubject = "New Contact Form Submission: " . htmlspecialchars($subject);
|
|
$ownerHtmlBody = "
|
|
<h2>New message from your website contact form:</h2>
|
|
<p><strong>Name:</strong> " . htmlspecialchars($name) . "</p>
|
|
<p><strong>Email:</strong> " . htmlspecialchars($email) . "</p>
|
|
<p><strong>Subject:</strong> " . htmlspecialchars($subject) . "</p>
|
|
<p><strong>Message:</strong></p>
|
|
<p>" . nl2br(htmlspecialchars($message)) . "</p>
|
|
";
|
|
$ownerTextBody = "New message from your website contact form:\nName: $name\nEmail: $email\nSubject: $subject\nMessage:\n$message";
|
|
|
|
MailService::sendMail(
|
|
$siteOwnerEmail,
|
|
$ownerSubject,
|
|
$ownerHtmlBody,
|
|
$ownerTextBody,
|
|
['reply_to' => $email]
|
|
);
|
|
|
|
// 2. Auto-reply to Visitor
|
|
$visitorSubject = "Thank you for contacting " . $projectName;
|
|
$visitorHtmlBody = "
|
|
<h2>Thank You For Your Message!</h2>
|
|
<p>Hello " . htmlspecialchars($name) . ",</p>
|
|
<p>We have received your message and will get back to you as soon as possible.</p>
|
|
<p>Here is a copy of your submission:</p>
|
|
<hr>
|
|
<p><strong>Subject:</strong> " . htmlspecialchars($subject) . "</p>
|
|
<p><strong>Message:</strong></p>
|
|
<p>" . nl2br(htmlspecialchars($message)) . "</p>
|
|
<hr>
|
|
<p>Best regards,<br>" . $projectName . "</p>
|
|
";
|
|
$visitorTextBody = "Hello $name,\nThank you for your message! We have received it and will get back to you shortly.\n\nYour submission:\nSubject: $subject\nMessage: $message\n\nBest regards,\n" . $projectName;
|
|
|
|
MailService::sendMail(
|
|
$email,
|
|
$visitorSubject,
|
|
$visitorHtmlBody,
|
|
$visitorTextBody
|
|
);
|
|
|
|
|
|
// --- Success Response ---
|
|
echo json_encode(['success' => true]);
|
|
|