honeypot ?? $params->website_hp ?? $params->website ?? ''; if (!empty($honeypot)) { // Silently discard bot submission with 200 OK http_response_code(200); echo json_encode(["status" => "success"]); exit; } // Sanitize & validate incoming fields $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'); if (!$email || empty($text)) { http_response_code(400); echo json_encode(["status" => "error", "message" => "Invalid input data"]); exit; } // Render HTML email template $rawMessage = $params->message ?? ''; $emailBody = renderEmailTemplate($name, $email, $text, $rawMessage); $subject = "{$config['app_name']}: $name"; // Construct standard MIME headers $headers = [ 'MIME-Version: 1.0', 'Content-type: text/html; charset=utf-8', "From: {$config['from_email']}", "Reply-To: $email" ]; // Send email via PHP native mailer $success = mail($config['recipient_email'], $subject, $emailBody, implode("\r\n", $headers)); if ($success) { http_response_code(200); echo json_encode(["status" => "success"]); } else { http_response_code(500); echo json_encode(["status" => "error", "message" => "Failed to send email"]); } break; // 3. Reject Unsupported HTTP Methods default: header("Allow: POST, OPTIONS", true, 405); http_response_code(405); echo json_encode(["status" => "error", "message" => "Method Not Allowed"]); exit; }