'Method not allowed']); exit; } $data = json_decode(file_get_contents('php://input'), true); if (!$data) { http_response_code(400); echo json_encode(['error' => 'Invalid JSON']); exit; } $name = htmlspecialchars(trim($data['name'] ?? ''), ENT_QUOTES); $phone = htmlspecialchars(trim($data['phone'] ?? ''), ENT_QUOTES); $email = htmlspecialchars(trim($data['email'] ?? ''), ENT_QUOTES); $message = htmlspecialchars(trim($data['message'] ?? ''), ENT_QUOTES); if (!$name || !$message) { http_response_code(400); echo json_encode(['error' => 'Missing required fields']); exit; } $BOT_TOKEN = '8138813013:AAElH2L5NspRLSdiFjDz6Qf32n4G24P_cj8'; $CHAT_ID = '-5230603582'; $text = "🔔 Заявка с sag24.ru\n\n"; $text .= "👤 Имя: {$name}\n"; if ($phone) $text .= "📱 Телефон: {$phone}\n"; if ($email) $text .= "📧 Email: {$email}\n"; $text .= "\n💬 Сообщение:\n{$message}\n"; $text .= "\n⏰ " . date('d.m.Y H:i', time() + 3 * 3600) . " MSK"; $payload = json_encode([ 'chat_id' => $CHAT_ID, 'text' => $text, 'parse_mode' => 'HTML', ]); $ch = curl_init("https://api.telegram.org/bot{$BOT_TOKEN}/sendMessage"); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => $payload, CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10, ]); $result = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); // Email fallback (always send, regardless of Telegram result) $subject = "Заявка с сag24.ru от {$name}"; $body = "Имя: {$name}\n"; if ($phone) $body .= "Телефон: {$phone}\n"; if ($email) $body .= "Email: {$email}\n"; $body .= "\nСообщение:\n{$message}\n"; $body .= "\n" . date('d.m.Y H:i', time() + 3 * 3600) . " MSK"; $headers = "From: noreply@sag24.ru\r\nContent-Type: text/plain; charset=UTF-8"; mail('info@sag24.ru', $subject, $body, $headers); // Return success regardless of Telegram result echo json_encode(['success' => true]);