41 lines
1.5 KiB
PHP
41 lines
1.5 KiB
PHP
<?php
|
|
session_start();
|
|
require_once __DIR__ . '/mail/MailService.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$response = ['success' => false, 'message' => 'An unexpected error occurred.'];
|
|
|
|
$userName = 'An anonymous user';
|
|
$userEmail = 'Not provided';
|
|
|
|
if (isset($_SESSION['user_id'])) {
|
|
$userName = $_SESSION['user_name'];
|
|
$userEmail = $_SESSION['user_email'];
|
|
}
|
|
|
|
$subject = 'SOS Alert Triggered by ' . $userName;
|
|
$htmlBody = "<p>An SOS alert was triggered by:</p>"
|
|
. "<ul>"
|
|
. "<li><strong>Name:</strong> " . htmlspecialchars($userName) . "</li>"
|
|
. "<li><strong>Email:</strong> " . htmlspecialchars($userEmail) . "</li>"
|
|
. "<li><strong>Time:</strong> " . date('Y-m-d H:i:s') . " UTC</li>"
|
|
. "</ul>"
|
|
. "<p>Please take appropriate action immediately.</p>";
|
|
$textBody = "SOS Alert from " . $userName . " (" . $userEmail . ") at " . date('Y-m-d H:i:s') . " UTC.";
|
|
|
|
// Send the email. If no recipient is set in .env, it will use the default.
|
|
$result = MailService::sendMail(null, $subject, $htmlBody, $textBody);
|
|
|
|
if (!empty($result['success'])) {
|
|
$response['success'] = true;
|
|
$response['message'] = 'SOS alert sent successfully. Help is on the way.';
|
|
} else {
|
|
// In a real app, you would log this error in more detail.
|
|
$response['message'] = 'Failed to send SOS alert. Please try again or contact support.';
|
|
// For debugging, you might want to include the error, but not in production.
|
|
// $response['error'] = $result['error'];
|
|
}
|
|
|
|
echo json_encode($response);
|