Канал алерта — Telegram напрямую (curl, без xray: xray телегу не роутит, проверено). Восстановление — молча в лог, без пинга. email_service.send_alert откачен (не нужен). Профилактика: регулярный вызов держит OAuth-сессию тёплой. Тест обновлён. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
55 lines
2.4 KiB
Python
55 lines
2.4 KiB
Python
"""Email service — send verification codes via SMTP (Timeweb)."""
|
|
|
|
import logging
|
|
import os
|
|
import smtplib
|
|
import ssl
|
|
from email.mime.multipart import MIMEMultipart
|
|
from email.mime.text import MIMEText
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
SMTP_HOST = os.environ.get("SMTP_HOST", "smtp.timeweb.ru")
|
|
SMTP_PORT = int(os.environ.get("SMTP_PORT", "465"))
|
|
SMTP_USER = os.environ.get("SMTP_USER", "")
|
|
SMTP_PASS = os.environ.get("SMTP_PASS", "")
|
|
|
|
|
|
def send_verification_code(to_email: str, code: str) -> bool:
|
|
"""Send 6-digit verification code to email. Returns True on success."""
|
|
if not SMTP_USER or not SMTP_PASS:
|
|
logger.warning("SMTP not configured, skipping email to %s", to_email)
|
|
return False
|
|
|
|
subject = "Код подтверждения — Сметный ИИ-ассистент"
|
|
html = f"""
|
|
<div style="font-family: Arial, sans-serif; max-width: 400px; margin: 0 auto; padding: 20px;">
|
|
<h2 style="color: #3a3a3a;">Сметный ИИ-ассистент</h2>
|
|
<p>Ваш код подтверждения:</p>
|
|
<div style="background: #f5f0eb; padding: 15px; border-radius: 8px; text-align: center; margin: 20px 0;">
|
|
<span style="font-size: 32px; font-weight: bold; letter-spacing: 8px; color: #c4463a;">{code}</span>
|
|
</div>
|
|
<p style="color: #6b6b6b; font-size: 14px;">Код действителен 10 минут.</p>
|
|
<p style="color: #6b6b6b; font-size: 12px;">Если вы не регистрировались — проигнорируйте это письмо.</p>
|
|
</div>
|
|
"""
|
|
|
|
msg = MIMEMultipart("alternative")
|
|
msg["Subject"] = subject
|
|
from email.header import Header
|
|
msg["From"] = f"{Header('Сметный ассистент', 'utf-8').encode()} <{SMTP_USER}>"
|
|
msg["To"] = to_email
|
|
msg.attach(MIMEText(f"Ваш код подтверждения: {code}\nДействителен 10 минут.", "plain", "utf-8"))
|
|
msg.attach(MIMEText(html, "html", "utf-8"))
|
|
|
|
try:
|
|
context = ssl.create_default_context()
|
|
with smtplib.SMTP_SSL(SMTP_HOST, SMTP_PORT, context=context, timeout=10) as server:
|
|
server.login(SMTP_USER, SMTP_PASS)
|
|
server.sendmail(SMTP_USER, to_email, msg.as_string())
|
|
logger.info("Verification email sent to %s", to_email)
|
|
return True
|
|
except Exception as e:
|
|
logger.error("Failed to send email to %s: %s", to_email, e)
|
|
return False
|