"""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"""\
Thank you for purchasing the Nomad Code ebook!
Download your files:
If you have any issues, reply to this email.
— Nomad Code
""" 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