website)) { http_response_code(200); echo json_encode(["status" => "success"]); exit; } // 2. Sanitize inputs & set fallbacks $email = filter_var($params->email ?? '', FILTER_VALIDATE_EMAIL); $name = htmlspecialchars(trim($params->name ?? 'Anonymous'), ENT_QUOTES, 'UTF-8'); $text = htmlspecialchars(trim($params->message ?? ''), ENT_QUOTES, 'UTF-8'); // Validate required fields if (!$email || empty($text)) { http_response_code(400); echo json_encode(["status" => "error", "message" => "Invalid input"]); exit; } // 3. Render template $emailBody = renderEmailTemplate($name, $email, $text, $params->message ?? ''); $subject = "{$config['app_name']}: $name"; // 4. Set headers $headers = [ 'MIME-Version: 1.0', 'Content-type: text/html; charset=utf-8', "From: {$config['from_email']}", "Reply-To: $email" ]; // Send email $success = mail($config['recipient_email'], $subject, $emailBody, implode("\n", $headers)); if ($success) { http_response_code(200); echo json_encode(["status" => "success"]); } else { http_response_code(500); echo json_encode(["status" => "error"]); } break; default: // Reject any other HTTP method header("Allow: POST", true, 405); exit; }