nomadro/apps/ebook/backend/app/services/email.py
eric d93a355fd5 Migrate navmini/ebook/nomadweb into apps/ as same-origin portal subpaths.
Shield external portal jumps (keep EXTERNAL_* for later), serve /nav from static build, and proxy /book + /community to vertical Next apps with Caddy notes.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 03:30:34 -05:00

55 lines
1.5 KiB
Python

"""Send transactional emails via SMTP."""
import smtplib
import ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from ..config import get_settings
def send_purchase_email(to_email: str, customer_name: str | None = None) -> bool:
s = get_settings()
if not s.smtp_host or not s.smtp_user:
return False
name = customer_name or "Reader"
msg = MIMEMultipart("alternative")
msg["Subject"] = "Your Nomad Code ebook — download links"
msg["From"] = s.smtp_from
msg["To"] = to_email
text = f"""Hi {name},
Thank you for purchasing the Nomad Code ebook!
Download your files here:
PDF: {s.download_base_url}/nomad-code.pdf
EPUB: {s.download_base_url}/nomad-code.epub
If you have any issues, reply to this email.
— Nomad Code
"""
html = f"""\
<html><body style="font-family:sans-serif;line-height:1.6;color:#333">
<h2>Hi {name},</h2>
<p>Thank you for purchasing the <strong>Nomad Code</strong> ebook!</p>
<p>Download your files:</p>
<ul>
<li><a href="{s.download_base_url}/nomad-code.pdf">PDF</a></li>
<li><a href="{s.download_base_url}/nomad-code.epub">EPUB</a></li>
</ul>
<p>If you have any issues, reply to this email.</p>
<p>— Nomad Code</p>
</body></html>
"""
msg.attach(MIMEText(text, "plain"))
msg.attach(MIMEText(html, "html"))
ctx = ssl.create_default_context()
with smtplib.SMTP_SSL(s.smtp_host, s.smtp_port, context=ctx) as server:
server.login(s.smtp_user, s.smtp_pass)
server.sendmail(s.smtp_from, to_email, msg.as_string())
return True