| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- # -*- coding: utf-8 -*-
- """Steady 品牌邮件模板(table 布局,兼容 QQ 邮箱/Gmail/Outlook/Apple Mail)。
- 品牌色与站点一致:暖纸底 #f6f2e9、墨色 #17140f、橙 #ff5c2b。
- 每个模板函数返回 (subject_suffix_free, html, text) 中的 html / text 片段,
- 正文页头页脚统一由 _shell 包裹。
- """
- import html as html_lib
- import os
- import re
- # ---------- 品牌常量 ----------
- BRAND = "Steady"
- BRAND_FULL = "Steady Packaging"
- TAGLINE = "为小批量品牌而生的包装设计工作室"
- PAPER = "#f6f2e9"
- CARD = "#fffdf8"
- INK = "#17140f"
- MUTED = "#8a8070"
- LINE = "#e6ddcd"
- ACCENT = "#ff5c2b"
- ACCENT_DARK = "#e64718"
- ACCENT_SOFT = "#fff2ec"
- # 注意:字体名用单引号——本模块所有样式都写在 style="..." 双引号属性内,
- # 双引号会截断 HTML 属性,导致后续 CSS 声明失效(邮件客户端里按钮变蓝下划线)。
- FONT = ("-apple-system,BlinkMacSystemFont,'Segoe UI','PingFang SC',"
- "'Hiragino Sans GB','Microsoft YaHei',Arial,sans-serif")
- def site_url() -> str:
- return os.environ.get("PL_SITE_URL", "https://steadypackaging.com").rstrip("/")
- def _esc(s) -> str:
- return html_lib.escape(str(s or ""))
- def _linkify(safe: str) -> str:
- """在已转义文本上把 http(s) 链接变可点。"""
- return re.sub(r"(https?://[^\s<]+)", r'<a href="\1" style="color:%s;text-decoration:underline">\1</a>' % ACCENT, safe)
- def _nl2br(text: str) -> str:
- paras = [p.strip() for p in re.split(r"\n[ \t]*\n", text)]
- out = []
- for p in paras:
- if not p:
- continue
- out.append(
- '<p style="margin:0 0 14px;font-size:15px;line-height:1.8;color:%s;">%s</p>'
- % (INK, _linkify(_esc(p).replace("\n", "<br>")))
- )
- return "\n".join(out)
- def _brand_header() -> str:
- return """
- <tr>
- <td style="padding:28px 40px 20px;border-bottom:1px solid %s;">
- <table role="presentation" cellpadding="0" cellspacing="0" border="0">
- <tr>
- <td width="38" height="38" align="center" valign="middle" bgcolor="%s"
- style="border-radius:10px;font-family:%s;font-size:20px;font-weight:700;color:#fffdf8;letter-spacing:0;">S</td>
- <td style="padding-left:12px;line-height:1.25;">
- <div style="font-family:%s;font-size:18px;font-weight:700;color:%s;letter-spacing:.2px;">%s</div>
- <div style="font-family:%s;font-size:10px;font-weight:600;color:%s;letter-spacing:2.5px;margin-top:2px;">PACKAGING</div>
- </td>
- </tr>
- </table>
- </td>
- </tr>""" % (LINE, ACCENT, FONT, FONT, INK, BRAND, FONT, MUTED)
- def _footer() -> str:
- url = site_url()
- return """
- <tr>
- <td style="padding:22px 40px 30px;border-top:1px solid %s;">
- <p style="margin:0 0 6px;font-size:12px;line-height:1.7;color:%s;">
- <strong style="color:%s;">%s</strong> · %s
- </p>
- <p style="margin:0;font-size:12px;line-height:1.7;color:%s;">
- © 2026 Steady Packaging |
- <a href="%s" style="color:%s;text-decoration:none;">%s</a>
- </p>
- </td>
- </tr>""" % (LINE, MUTED, INK, BRAND_FULL, TAGLINE, MUTED, url, MUTED,
- re.sub(r"^https?://", "", url))
- def _shell(inner: str, preheader: str = "") -> str:
- """整页外壳:暖纸底 + 居中卡片。"""
- return """<!doctype html>
- <html lang="zh-CN">
- <head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
- <title>%s</title></head>
- <body style="margin:0;padding:0;background-color:%s;">
- <div style="display:none;max-height:0;overflow:hidden;opacity:0;">%s</div>
- <table role="presentation" width="100%%" cellpadding="0" cellspacing="0" border="0" bgcolor="%s" style="background-color:%s;">
- <tr><td align="center" style="padding:32px 16px;">
- <table role="presentation" width="600" cellpadding="0" cellspacing="0" border="0"
- style="width:600px;max-width:600px;background-color:%s;border:1px solid %s;border-radius:16px;overflow:hidden;">
- %s
- %s
- %s
- </table>
- <p style="margin:16px 0 0;font-size:11px;color:%s;font-family:%s;">
- Steady Packaging 系统通知邮件
- </p>
- </td></tr>
- </table>
- </body></html>""" % (
- _esc(preheader), PAPER, _esc(preheader), PAPER, PAPER, CARD, LINE,
- _brand_header(), inner, _footer(), MUTED, FONT,
- )
- # ---------- 通用组件 ----------
- def _button(label: str, url: str) -> str:
- return """
- <tr><td style="padding:6px 0 4px;">
- <table role="presentation" cellpadding="0" cellspacing="0" border="0" align="center" style="margin:0 auto;">
- <tr><td align="center" bgcolor="%s" style="border-radius:10px;">
- <a href="%s" target="_blank"
- style="display:inline-block;padding:13px 34px;font-family:%s;font-size:15px;font-weight:600;color:#fffdf8;text-decoration:none;border-radius:10px;background-color:%s;">%s</a>
- </td></tr>
- </table>
- </td></tr>""" % (ACCENT, _esc(url), FONT, ACCENT, _esc(label))
- def _meta_rows(rows: list) -> str:
- """rows: [(label, value, value_strong_bool)],渲染成信息卡。"""
- items = []
- for label, value in rows:
- items.append("""
- <tr>
- <td style="padding:9px 0;font-size:13px;color:%s;white-space:nowrap;vertical-align:top;">%s</td>
- <td width="16"></td>
- <td align="right" style="padding:9px 0;font-size:13px;font-weight:600;color:%s;text-align:right;">%s</td>
- </tr>""" % (MUTED, _esc(label), INK, _esc(value)))
- return """
- <tr><td style="padding:20px 40px 4px;">
- <table role="presentation" width="100%%" cellpadding="0" cellspacing="0" border="0"
- style="background-color:%s;border:1px solid %s;border-radius:12px;">
- <tr><td style="padding:6px 18px;">
- <table role="presentation" width="100%%" cellpadding="0" cellspacing="0" border="0">
- %s
- </table>
- </td></tr>
- </table>
- </td></tr>""" % (ACCENT_SOFT, "#f3d8c8", "\n".join(items))
- # ---------- 场景一:登录验证码 ----------
- def verification_code(code: str, minutes: int = 10) -> tuple:
- """返回 (html, text)。"""
- inner = """
- <tr><td style="padding:34px 40px 8px;">
- <h1 style="margin:0 0 10px;font-family:%s;font-size:22px;font-weight:700;color:%s;">登录验证码</h1>
- <p style="margin:0 0 24px;font-size:15px;line-height:1.8;color:%s;">
- 你正在登录 <strong>Steady Packaging</strong>,请在验证码输入框填写以下 %d 位数字:
- </p>
- <table role="presentation" width="100%%" cellpadding="0" cellspacing="0" border="0">
- <tr><td align="center" bgcolor="%s"
- style="border:1px dashed %s;border-radius:12px;padding:22px 10px;">
- <span style="font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;
- font-size:36px;font-weight:700;letter-spacing:10px;color:%s;padding-left:10px;">%s</span>
- </td></tr>
- </table>
- <p style="margin:22px 0 0;font-size:13px;line-height:1.8;color:%s;">
- · 验证码 %d 分钟内有效,过期可重新获取<br>
- · 请勿将验证码转发给任何人,Steady 工作人员不会向你索取<br>
- · 如非本人操作,请忽略本邮件,你的账户安全不受影响
- </p>
- </td></tr>""" % (
- FONT, INK, INK, len(str(code)), ACCENT_SOFT, ACCENT, INK, _esc(code),
- MUTED, minutes,
- )
- html = _shell(inner, preheader="验证码 %s,%d 分钟内有效" % (code, minutes))
- text = (
- "【Steady】登录验证码\n\n"
- "你的登录验证码:%s(%d 分钟内有效)\n"
- "请勿将验证码转发给任何人。如非本人操作请忽略。\n\n"
- "Steady Packaging · %s" % (code, minutes, site_url())
- )
- return html, text
- # ---------- 场景二:订单沟通邮件(后台发给用户) ----------
- def order_message(subject: str, body: str, order: dict | None = None,
- title: str | None = None) -> tuple:
- """order 可含 order_no/summary/total/contact_name;返回 (html, text)。"""
- order = order or {}
- order_no = order.get("order_no") or ""
- if not title:
- title = re.sub(r"^【[^】]*】\s*", "", subject).strip() or "订单通知"
- rows = []
- if order_no:
- rows.append(("订单号", order_no))
- if order.get("total") not in (None, ""):
- try:
- rows.append(("订单金额", "¥%s" % format(float(order["total"]), ",.2f")))
- except (TypeError, ValueError):
- pass
- meta = _meta_rows(rows) if rows else ""
- cta = ""
- if order_no:
- cta = _button("查看我的订单", site_url() + "/orders.html")
- inner = """
- <tr><td style="padding:34px 40px 8px;">
- <h1 style="margin:0 0 18px;font-family:%s;font-size:21px;font-weight:700;color:%s;line-height:1.4;">%s</h1>
- %s
- </td></tr>
- %s
- %s
- <tr><td style="padding:18px 40px 0;">
- <p style="margin:0;font-size:13px;line-height:1.8;color:%s;">
- 你可以直接回复本邮件,或登录后在「我的订单」中查看进度。
- </p>
- </td></tr>""" % (
- FONT, INK, _esc(title), _nl2br(body), meta, cta, MUTED,
- )
- html = _shell(inner, preheader="订单 %s 有新的进展通知" % order_no)
- text_lines = ["【Steady】" + re.sub(r"^【[^】]*】\s*", "", subject).strip(), "", body.strip()]
- if rows:
- text_lines += ["", " ".join("%s:%s" % (k, v) for k, v in rows)]
- text_lines += ["", "查看订单:" + site_url() + "/orders.html", "", "Steady Packaging · " + site_url()]
- return html, "\n".join(text_lines)
|