37 lines
1.4 KiB
PHP
37 lines
1.4 KiB
PHP
|
|
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/mail/MailService.php';
|
|
|
|
$response = ['success' => false];
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$name = isset($_POST['name']) ? trim($_POST['name']) : '';
|
|
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
|
|
$message = isset($_POST['message']) ? trim($_POST['message']) : '';
|
|
|
|
if (!empty($name) && filter_var($email, FILTER_VALIDATE_EMAIL) && !empty($message)) {
|
|
// The 'to' address can be omitted to use the default from .env
|
|
// Or specified directly. For this example, we'll use a placeholder
|
|
// that should be configured in a real environment.
|
|
$to = getenv('MAIL_TO') ?: 'support@yourdomain.com';
|
|
$subject = 'New Contact Form Submission';
|
|
|
|
$mailResult = MailService::sendContactMessage($name, $email, $message, $to, $subject);
|
|
|
|
if (!empty($mailResult['success'])) {
|
|
$response['success'] = true;
|
|
} else {
|
|
$response['error'] = $mailResult['error'] ?? 'Failed to send email.';
|
|
// In a real app, you would log this error.
|
|
// error_log('MailService Error: ' . $response['error']);
|
|
}
|
|
} else {
|
|
$response['error'] = 'Invalid input. Please fill out all fields correctly.';
|
|
}
|
|
} else {
|
|
$response['error'] = 'Invalid request method.';
|
|
}
|
|
|
|
echo json_encode($response);
|