- Always send email to info@sag24.ru (via server mail()) - Return success even if Telegram is unreachable (ТСПУ blocks 45.10.53.x) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
71 lines
2.4 KiB
PHP
71 lines
2.4 KiB
PHP
<?php
|
||
header('Content-Type: application/json');
|
||
header('Access-Control-Allow-Origin: https://sag24.ru');
|
||
header('Access-Control-Allow-Methods: POST');
|
||
header('Access-Control-Allow-Headers: Content-Type');
|
||
|
||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||
http_response_code(405);
|
||
echo json_encode(['error' => '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 = "🔔 <b>Заявка с sag24.ru</b>\n\n";
|
||
$text .= "👤 <b>Имя:</b> {$name}\n";
|
||
if ($phone) $text .= "📱 <b>Телефон:</b> {$phone}\n";
|
||
if ($email) $text .= "📧 <b>Email:</b> {$email}\n";
|
||
$text .= "\n💬 <b>Сообщение:</b>\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]);
|