44 lines
1.8 KiB
PHP
44 lines
1.8 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
require_once __DIR__ . '/mail/MailService.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$name = filter_var($_POST['name'] ?? '', FILTER_SANITIZE_STRING);
|
|
$email = filter_var($_POST['email'] ?? '', FILTER_SANITIZE_EMAIL);
|
|
$message = filter_var($_POST['message'] ?? '', FILTER_SANITIZE_STRING);
|
|
|
|
if (empty($name) || empty($email) || empty($message)) {
|
|
echo json_encode(['success' => false, 'message' => 'Please fill out all fields.']);
|
|
exit;
|
|
}
|
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
echo json_encode(['success' => false, 'message' => 'Invalid email format.']);
|
|
exit;
|
|
}
|
|
|
|
$to = getenv('MAIL_TO') ?: 'your-email@example.com'; // Fallback email
|
|
$subject = 'New Contact Form Submission from ' . $name;
|
|
|
|
$html_content = "<p>You have received a new message from your website contact form.</p>";
|
|
$html_content .= "<p><strong>Name:</strong> {$name}</p>";
|
|
$html_content .= "<p><strong>Email:</strong> {$email}</p>";
|
|
$html_content .= "<p><strong>Message:</strong><br>{$message}</p>";
|
|
|
|
$text_content = "You have received a new message from your website contact form.\n";
|
|
$text_content .= "Name: {$name}\n";
|
|
$text_content .= "Email: {$email}\n";
|
|
$text_content .= "Message:\n{$message}";
|
|
|
|
$result = MailService::sendMail($to, $subject, $html_content, $text_content, ['reply_to' => $email]);
|
|
|
|
if ($result['success']) {
|
|
echo json_encode(['success' => true, 'message' => 'Thank you for your message. It has been sent.']);
|
|
} else {
|
|
echo json_encode(['success' => false, 'message' => 'Sorry, there was an error sending your message. Please try again later.']);
|
|
}
|
|
} else {
|
|
echo json_encode(['success' => false, 'message' => 'Invalid request method.']);
|
|
}
|