fix: contact.php — SMTP via noreply@sag24.ru, Telegram best-effort

- Send email via SMTP AUTH to mx.hhivp.com using noreply@sag24.ru
- Telegram is non-fatal (ТСПУ blocks 45.10.53.x datacenter)
- Form always returns success as long as email is sent

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-15 01:18:13 +03:00
parent cd265e4459
commit fc1d2c20cd

View File

@@ -28,6 +28,7 @@ if (!$name || !$message) {
exit; exit;
} }
// ─── Telegram (best-effort, not fatal) ───────────────────────────────────────
$BOT_TOKEN = '8138813013:AAElH2L5NspRLSdiFjDz6Qf32n4G24P_cj8'; $BOT_TOKEN = '8138813013:AAElH2L5NspRLSdiFjDz6Qf32n4G24P_cj8';
$CHAT_ID = '-5230603582'; $CHAT_ID = '-5230603582';
@@ -38,33 +39,55 @@ if ($email) $text .= "📧 <b>Email:</b> {$email}\n";
$text .= "\n💬 <b>Сообщение:</b>\n{$message}\n"; $text .= "\n💬 <b>Сообщение:</b>\n{$message}\n";
$text .= "\n" . date('d.m.Y H:i', time() + 3 * 3600) . " MSK"; $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"); $ch = curl_init("https://api.telegram.org/bot{$BOT_TOKEN}/sendMessage");
curl_setopt_array($ch, [ curl_setopt_array($ch, [
CURLOPT_POST => true, CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload, CURLOPT_POSTFIELDS => json_encode(['chat_id' => $CHAT_ID, 'text' => $text, 'parse_mode' => 'HTML']),
CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10, CURLOPT_TIMEOUT => 10,
]); ]);
$result = curl_exec($ch); curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); curl_close($ch);
// Email fallback (always send, regardless of Telegram result) // ─── Email via SMTP (noreply@sag24.ru → info@sag24.ru) ───────────────────────
$subject = "Заявка с сag24.ru от {$name}"; $smtp_host = 'mx.hhivp.com';
$body = "Имя: {$name}\n"; $smtp_port = 587;
if ($phone) $body .= "Телефон: {$phone}\n"; $smtp_user = 'noreply@sag24.ru';
if ($email) $body .= "Email: {$email}\n"; $smtp_pass = '9hsnDyBAk5&S4#lE';
$body .= "\nСообщение:\n{$message}\n"; $mail_to = 'info@sag24.ru';
$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"; $subject = "=?UTF-8?B?" . base64_encode("Заявка с sag24.ru от {$name}") . "?=";
mail('info@sag24.ru', $subject, $body, $headers); $body = "Имя: {$name}\r\n";
if ($phone) $body .= "Телефон: {$phone}\r\n";
if ($email) $body .= "Email: {$email}\r\n";
$body .= "\r\nСообщение:\r\n{$message}\r\n";
$body .= "\r\n" . date('d.m.Y H:i', time() + 3 * 3600) . " MSK";
try {
$smtp = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 10);
if ($smtp) {
fgets($smtp, 512);
fwrite($smtp, "EHLO sag24.ru\r\n"); $caps = '';
while ($line = fgets($smtp, 512)) { $caps .= $line; if ($line[3] == ' ') break; }
fwrite($smtp, "STARTTLS\r\n"); fgets($smtp, 512);
stream_socket_enable_crypto($smtp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
fwrite($smtp, "EHLO sag24.ru\r\n");
while ($line = fgets($smtp, 512)) { if ($line[3] == ' ') break; }
fwrite($smtp, "AUTH LOGIN\r\n"); fgets($smtp, 512);
fwrite($smtp, base64_encode($smtp_user) . "\r\n"); fgets($smtp, 512);
fwrite($smtp, base64_encode($smtp_pass) . "\r\n"); fgets($smtp, 512);
fwrite($smtp, "MAIL FROM:<{$smtp_user}>\r\n"); fgets($smtp, 512);
fwrite($smtp, "RCPT TO:<{$mail_to}>\r\n"); fgets($smtp, 512);
fwrite($smtp, "DATA\r\n"); fgets($smtp, 512);
fwrite($smtp, "From: noreply@sag24.ru\r\nTo: {$mail_to}\r\nSubject: {$subject}\r\n");
fwrite($smtp, "MIME-Version: 1.0\r\nContent-Type: text/plain; charset=UTF-8\r\n\r\n");
fwrite($smtp, $body . "\r\n.\r\n"); fgets($smtp, 512);
fwrite($smtp, "QUIT\r\n");
fclose($smtp);
}
} catch (Throwable $e) {
// email failure is non-fatal
}
// Return success regardless of Telegram result
echo json_encode(['success' => true]); echo json_encode(['success' => true]);