"""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"""
Click the button below to sign in. This link expires in 15 minutes.
If you didn't request this, ignore this email.
{title} produced {clip_count} viral-ready clip{'s' if clip_count != 1 else ''}. They're waiting in your dashboard.
Enjoying OpenShorts? A star on GitHub helps a lot ⭐
Free clips are stored for 7 days, and {('these' if n != 1 else 'this one')} {'are' if n != 1 else 'is'} about to expire. Two ways to keep them:
After tomorrow they're gone for good — we can't recover deleted clips.
You've used this month's 20 free minutes — which usually means the clips are working for you. Here's what Starter ($12/mo) changes today:
Cancel anytime. Your free minutes reset on the 1st of every month.