- main.py / metering.py: try the HD (bgutil/720p) strategy first, fall back to the conservative (360p) clients on any failure, so a YouTube/yt-dlp change degrades gracefully instead of failing the whole job. - cloud/alerts.py: in-memory tracking of managed-job outcomes; emails the admin (ADMIN_EMAIL) when a proxy/credit error appears or when many jobs fail in a row, with a 1h per-alert cooldown. Wired into run_job_wrapper. - Email switched from Resend to SMTP (cloud/emails.py) for magic link + alerts (Namecheap Private Email). Config via SMTP_* / EMAIL_FROM / ADMIN_EMAIL in the mounted .env (kept out of compose so the password's $/* aren't mangled). SMTP send verified from host and from the backend container.
58 lines
2.3 KiB
Python
58 lines
2.3 KiB
Python
"""Transactional + operational email via SMTP (e.g. Namecheap Private Email).
|
|
|
|
If SMTP isn't configured (local dev), messages are printed to the server log so
|
|
the magic-link flow and alerts still work without a real mailbox.
|
|
"""
|
|
import asyncio
|
|
import smtplib
|
|
import ssl
|
|
from email.message import EmailMessage
|
|
|
|
from .config import settings
|
|
|
|
|
|
def _send_sync(to: str, subject: str, html: str):
|
|
msg = EmailMessage()
|
|
msg["From"] = settings.email_from
|
|
msg["To"] = to
|
|
msg["Subject"] = subject
|
|
msg.set_content("This message requires an HTML-capable email client.")
|
|
msg.add_alternative(html, subtype="html")
|
|
|
|
if settings.smtp_port == 465:
|
|
with smtplib.SMTP_SSL(settings.smtp_host, settings.smtp_port,
|
|
context=ssl.create_default_context(), timeout=20) as s:
|
|
s.login(settings.smtp_user, settings.smtp_password)
|
|
s.send_message(msg)
|
|
else:
|
|
with smtplib.SMTP(settings.smtp_host, settings.smtp_port, timeout=20) as s:
|
|
s.starttls(context=ssl.create_default_context())
|
|
s.login(settings.smtp_user, settings.smtp_password)
|
|
s.send_message(msg)
|
|
|
|
|
|
async def send_email(to: str, subject: str, html: str):
|
|
"""Send an email (async wrapper). Logs instead of sending if SMTP is unset."""
|
|
if not settings.smtp_configured:
|
|
print(f"✉️ [DEV email → {to}] {subject}")
|
|
return
|
|
try:
|
|
await asyncio.to_thread(_send_sync, to, subject, html)
|
|
except Exception as e:
|
|
print(f"⚠️ Failed to send email to {to}: {e}")
|
|
|
|
|
|
async def send_magic_link_email(email: str, link: str):
|
|
if not settings.smtp_configured:
|
|
print(f"✉️ [DEV magic link] {email} -> {link}")
|
|
return
|
|
html = f"""
|
|
<div style="font-family:system-ui,sans-serif;max-width:480px;margin:0 auto">
|
|
<h2>Sign in to OpenShorts</h2>
|
|
<p>Click the button below to sign in. This link expires in 15 minutes.</p>
|
|
<p><a href="{link}" style="display:inline-block;background:#111;color:#fff;
|
|
padding:12px 20px;border-radius:8px;text-decoration:none">Sign in</a></p>
|
|
<p style="color:#666;font-size:13px">If you didn't request this, ignore this email.</p>
|
|
</div>
|
|
"""
|
|
await send_email(email, "Your OpenShorts sign-in link", html)
|