"""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, headers: dict | None = None): msg = EmailMessage() msg["From"] = settings.email_from msg["To"] = to msg["Subject"] = subject for name, value in (headers or {}).items(): msg[name] = value 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, headers: dict | None = None): """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, headers) except Exception as e: print(f"⚠️ Failed to send email to {to}: {e}") async def send_commercial_email(user_id, to: str, subject: str, html: str) -> bool: """Send a commercial communication (LSSI art. 21): never to an account that unsubscribed, and always with a one-click way out (footer link plus the ``List-Unsubscribe`` header, see cloud/marketing.py). Returns False when the account had opted out and nothing was sent. """ from . import marketing if user_id is not None and await marketing.is_opted_out(user_id): return False unsub = marketing.unsubscribe_url(user_id) if user_id is not None else "" headers = {} if unsub: footer = (f'Unsubscribe from ' "upgrade and tips emails.") headers = {"List-Unsubscribe": f"<{unsub}>", "List-Unsubscribe-Post": "List-Unsubscribe=One-Click"} else: footer = "Reply to this email if you'd rather not get upgrade and tips emails." html = (f'{html}
{footer}
') await send_email(to, subject, html, headers) return True 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 ⭐
The best {scheduled_count} {'is' if scheduled_count == 1 else 'are'} " f"scheduled to publish, one a day, on the accounts you picked.
") else: posting = "Review them, tweak anything you like, and post the ones you want.
" html = f"""You published {title} and OpenShorts already turned it into {clip_count} short{plural}.
{posting}You can pause Autopilot anytime from the Autopilot page in your dashboard.
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.
Everything is gone: your account, your projects, your clips and their transcripts, your API keys, and the connection to any social accounts you had linked. Any active subscription was cancelled.
Two things we keep, and why:
You can sign up again any time with the same address; it will be a brand-new, empty account.
If this wasn't you, reply to this email straight away — info@openshorts.app.
Your first video is on us: anything up to " f"{first_video_minutes} minutes is clipped whole, free.
") return "You get 20 free minutes every month.
" async def send_welcome_email(user_id, email: str, first_video_minutes: int) -> bool: """Right after sign-up: one concrete next step, not a feature tour.""" app_url = f"{settings.frontend_url}/#app" html = f"""Paste a YouTube link (or upload a video) and you get vertical clips with captions, ready for TikTok, Reels and Shorts, in a few minutes.
{_first_video_line(first_video_minutes)} {_cta(app_url, "Clip my first video")}Works best with people talking: podcasts, interviews, streams, tutorials.
You signed up yesterday but haven't clipped anything yet. It takes one step: paste the link of a video you already published.
{_first_video_line(first_video_minutes)} {_cta(app_url, "Paste a link")}Stuck on something? Just reply to this email.
Here's {promo_label or 'a discount'}: use code " f"{promo_code} " f"at checkout.
") subject = f"{promo_label or 'A discount'} on OpenShorts, for you" else: offer = "" subject = "Keep your clips, lose the watermark" html = f"""Free clips carry a watermark and are deleted after 7 days. Starter ($12/mo) gives you 100 minutes a month, no watermark, and clips stored forever.
{offer} {_cta(pricing, "See plans")}Cancel anytime.
You started upgrading to {what} but the payment wasn't completed. Your cart is saved: one click takes you back to it.
{_cta(recovery_url, "Finish checkout")}Card declined? PayPal and other methods are on the same page. Reply to this email if something didn't work.