""" Sends transactional email via Resend's HTTP API — the only outbound dependency this backend has. Same service the main app uses (apps/bi/backend/services/contact_service.py), but a fully separate account/API key: this site is its own deployment with its own .env, not sharing secrets with the main product's backend. Data flow: to/subject/html_body (+ optional reply_to, PDF attachment bytes) → attachment base64-encoded if present → POST https://api.resend.com/emails → raises on failure, letting the calling route return a clear error to the visitor instead of silently dropping it """ import base64 from typing import Optional import httpx from config import Config RESEND_API_URL = "https://api.resend.com/emails" def send_email( to: str, subject: str, html_body: str, reply_to: Optional[str] = None, attachment_bytes: Optional[bytes] = None, attachment_filename: Optional[str] = None, ) -> None: """ Sends one email through Resend. Data flow: recipient/subject/html (+ optional reply-to and one PDF attachment) → JSON payload assembled, attachment base64-encoded if given → POST to Resend → raises HTTPStatusError on any non-2xx response """ if not Config.RESEND_API_KEY: raise RuntimeError("RESEND_API_KEY is not configured") payload = { "from": f"{Config.MAIL_SENDER_NAME} <{Config.MAIL_SENDER_ADDRESS}>", "to": [to], "subject": subject, "html": html_body, } if reply_to: payload["reply_to"] = reply_to if attachment_bytes and attachment_filename: payload["attachments"] = [{ "filename": attachment_filename, "content": base64.b64encode(attachment_bytes).decode("ascii"), }] response = httpx.post( RESEND_API_URL, headers={"Authorization": f"Bearer {Config.RESEND_API_KEY}"}, json=payload, timeout=20.0, ) response.raise_for_status()