170 lines
6.6 KiB
Python
170 lines
6.6 KiB
Python
"""
|
|
Builds the branded HTML email sent to a visitor who requests a copy of
|
|
their calculator results ("email me a copy" — CLAUDE_CALCULATOR.md §5a).
|
|
Table-based layout with every style inline, since most email clients
|
|
(Outlook especially) ignore <style> blocks and unsupported CSS — this is
|
|
the standard, if verbose, way to get a consistently-rendered branded
|
|
email across clients, the same approach a Mailchimp-generated template uses.
|
|
|
|
Data flow:
|
|
payload dict (see routes/calculator.py's _extract_shared_fields) →
|
|
every dynamic value HTML-escaped (this endpoint is public and
|
|
unauthenticated, so payload content can never be trusted as safe HTML
|
|
— the one exception is summary_text's own <strong> spans, rebuilt as
|
|
real tags after escaping everything else, so arbitrary HTML someone
|
|
posts directly to the endpoint can never render as anything but inert
|
|
escaped text) → one self-contained HTML string, no external CSS or JS
|
|
"""
|
|
|
|
import html
|
|
import re
|
|
|
|
from config import Config
|
|
|
|
_STRONG_TAG_RE = re.compile(r"</?strong>")
|
|
|
|
NAVY = "#0A1420"
|
|
ACCENT = "#9FE838"
|
|
INK = "#0D1526"
|
|
MUTED = "#6B7A99"
|
|
CARD_BG = "#F7FAF2"
|
|
CARD_BORDER = "#E1ECD0"
|
|
PAGE_BG = "#EEF1EC"
|
|
|
|
_FONT_STACK = "Arial, Helvetica, sans-serif"
|
|
_MONO_STACK = "'Courier New', Courier, monospace"
|
|
|
|
|
|
def _rich_text_to_html(raw: str) -> str:
|
|
"""
|
|
Converts calculator.js's summary text (a plain string with
|
|
<strong>...</strong> spans, built via innerHTML for on-screen display)
|
|
into safe HTML: every plain segment is escaped, and only the spans
|
|
that were actually wrapped in <strong> are re-wrapped as real tags —
|
|
the same split-and-rebuild approach pdf_service.py uses to render
|
|
them as real bold text instead of literal tag characters.
|
|
"""
|
|
segments = _STRONG_TAG_RE.split(raw or "")
|
|
parts = []
|
|
for i, segment in enumerate(segments):
|
|
escaped = html.escape(segment)
|
|
parts.append(f"<strong>{escaped}</strong>" if i % 2 == 1 else escaped)
|
|
return "".join(parts)
|
|
|
|
|
|
def _result_card(label: str, value: str) -> str:
|
|
return f"""<td width="50%" valign="top" style="padding:6px;">
|
|
<div style="background:{CARD_BG};border:1px solid {CARD_BORDER};border-radius:10px;padding:16px 18px;">
|
|
<div style="font-family:{_MONO_STACK};font-size:11px;letter-spacing:.06em;text-transform:uppercase;color:{MUTED};margin:0 0 4px;">{html.escape(label)}</div>
|
|
<div style="font-family:{_FONT_STACK};font-size:22px;font-weight:700;color:{INK};line-height:1.2;">{html.escape(value)}</div>
|
|
</div>
|
|
</td>"""
|
|
|
|
|
|
def _result_rows(results: list) -> str:
|
|
cards = [_result_card(str(row.get("label", "")), str(row.get("value", ""))) for row in results]
|
|
rows = []
|
|
for i in range(0, len(cards), 2):
|
|
pair = cards[i:i + 2]
|
|
if len(pair) == 1:
|
|
pair.append('<td width="50%"></td>')
|
|
rows.append(f"<tr>{''.join(pair)}</tr>")
|
|
return "".join(rows)
|
|
|
|
|
|
def build_results_email_html(payload: dict) -> str:
|
|
"""
|
|
Renders the full branded results email.
|
|
|
|
Data flow:
|
|
payload → header band (logo + wordmark on navy) → greeting +
|
|
summary paragraph → a 2-column card grid of the result label/value
|
|
pairs → a note that the full PDF is attached → footer → one HTML string
|
|
"""
|
|
title = html.escape(payload.get("title") or "Your Results")
|
|
summary_html = _rich_text_to_html(payload.get("summary_text") or "")
|
|
results = payload.get("results") or []
|
|
logo_url = f"{Config.SITE_BASE_URL}/assets/images/logo.png"
|
|
|
|
results_block = ""
|
|
if results:
|
|
results_block = f"""
|
|
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="margin:24px 0 4px;">
|
|
{_result_rows(results)}
|
|
</table>"""
|
|
|
|
return f"""<!doctype html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
</head>
|
|
<body style="margin:0;padding:0;background:{PAGE_BG};">
|
|
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:{PAGE_BG};padding:32px 16px;">
|
|
<tr>
|
|
<td align="center">
|
|
<table role="presentation" width="600" cellpadding="0" cellspacing="0" style="width:600px;max-width:100%;background:#FFFFFF;border-radius:16px;overflow:hidden;">
|
|
|
|
<!-- Header -->
|
|
<tr>
|
|
<td align="center" style="background:{NAVY};padding:28px 24px;">
|
|
<table role="presentation" cellpadding="0" cellspacing="0">
|
|
<tr>
|
|
<td valign="middle" style="padding-right:10px;">
|
|
<img src="{logo_url}" width="22" height="32" alt="" style="display:block;">
|
|
</td>
|
|
<td valign="middle">
|
|
<span style="font-family:{_FONT_STACK};font-size:18px;font-weight:700;color:{ACCENT};">Bettersight Intelligence</span>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
|
|
<!-- Body -->
|
|
<tr>
|
|
<td style="padding:32px 32px 8px;">
|
|
<div style="font-family:{_FONT_STACK};font-size:20px;font-weight:700;color:{INK};margin-bottom:14px;">{title}</div>
|
|
<div style="font-family:{_FONT_STACK};font-size:15px;line-height:1.6;color:{INK};">{summary_html}</div>
|
|
{results_block}
|
|
</td>
|
|
</tr>
|
|
|
|
<!-- PDF note -->
|
|
<tr>
|
|
<td style="padding:8px 32px 0;">
|
|
<div style="font-family:{_FONT_STACK};font-size:13.5px;line-height:1.6;color:{MUTED};border-top:1px solid {CARD_BORDER};padding-top:16px;margin-top:8px;">
|
|
📎 Your full results — including the basis for these numbers — are attached as a PDF.
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
|
|
<!-- CTA -->
|
|
<tr>
|
|
<td align="center" style="padding:28px 32px 32px;">
|
|
<table role="presentation" cellpadding="0" cellspacing="0">
|
|
<tr>
|
|
<td align="center" style="background:{ACCENT};border-radius:100px;">
|
|
<a href="mailto:{Config.MAIL_SENDER_ADDRESS}" style="display:inline-block;padding:14px 28px;font-family:{_FONT_STACK};font-size:14.5px;font-weight:700;color:{NAVY};text-decoration:none;">Have questions? Just reply</a>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
|
|
<!-- Footer -->
|
|
<tr>
|
|
<td style="background:{PAGE_BG};padding:20px 32px;">
|
|
<div style="font-family:{_FONT_STACK};font-size:12px;line-height:1.6;color:{MUTED};text-align:center;">
|
|
Bettersight Intelligence — built and run directly, no reseller in between.
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</body>
|
|
</html>"""
|