email_templates.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. # -*- coding: utf-8 -*-
  2. """Steady 品牌邮件模板(table 布局,兼容 QQ 邮箱/Gmail/Outlook/Apple Mail)。
  3. 品牌色与站点一致:暖纸底 #f6f2e9、墨色 #17140f、橙 #ff5c2b。
  4. 每个模板函数返回 (subject_suffix_free, html, text) 中的 html / text 片段,
  5. 正文页头页脚统一由 _shell 包裹。
  6. """
  7. import html as html_lib
  8. import os
  9. import re
  10. # ---------- 品牌常量 ----------
  11. BRAND = "Steady"
  12. BRAND_FULL = "Steady Packaging"
  13. TAGLINE = "为小批量品牌而生的包装设计工作室"
  14. PAPER = "#f6f2e9"
  15. CARD = "#fffdf8"
  16. INK = "#17140f"
  17. MUTED = "#8a8070"
  18. LINE = "#e6ddcd"
  19. ACCENT = "#ff5c2b"
  20. ACCENT_DARK = "#e64718"
  21. ACCENT_SOFT = "#fff2ec"
  22. # 注意:字体名用单引号——本模块所有样式都写在 style="..." 双引号属性内,
  23. # 双引号会截断 HTML 属性,导致后续 CSS 声明失效(邮件客户端里按钮变蓝下划线)。
  24. FONT = ("-apple-system,BlinkMacSystemFont,'Segoe UI','PingFang SC',"
  25. "'Hiragino Sans GB','Microsoft YaHei',Arial,sans-serif")
  26. def site_url() -> str:
  27. return os.environ.get("PL_SITE_URL", "https://steadypackaging.com").rstrip("/")
  28. def _esc(s) -> str:
  29. return html_lib.escape(str(s or ""))
  30. def _linkify(safe: str) -> str:
  31. """在已转义文本上把 http(s) 链接变可点。"""
  32. return re.sub(r"(https?://[^\s&lt;]+)", r'<a href="\1" style="color:%s;text-decoration:underline">\1</a>' % ACCENT, safe)
  33. def _nl2br(text: str) -> str:
  34. paras = [p.strip() for p in re.split(r"\n[ \t]*\n", text)]
  35. out = []
  36. for p in paras:
  37. if not p:
  38. continue
  39. out.append(
  40. '<p style="margin:0 0 14px;font-size:15px;line-height:1.8;color:%s;">%s</p>'
  41. % (INK, _linkify(_esc(p).replace("\n", "<br>")))
  42. )
  43. return "\n".join(out)
  44. def _brand_header() -> str:
  45. return """
  46. <tr>
  47. <td style="padding:28px 40px 20px;border-bottom:1px solid %s;">
  48. <table role="presentation" cellpadding="0" cellspacing="0" border="0">
  49. <tr>
  50. <td width="38" height="38" align="center" valign="middle" bgcolor="%s"
  51. style="border-radius:10px;font-family:%s;font-size:20px;font-weight:700;color:#fffdf8;letter-spacing:0;">S</td>
  52. <td style="padding-left:12px;line-height:1.25;">
  53. <div style="font-family:%s;font-size:18px;font-weight:700;color:%s;letter-spacing:.2px;">%s</div>
  54. <div style="font-family:%s;font-size:10px;font-weight:600;color:%s;letter-spacing:2.5px;margin-top:2px;">PACKAGING</div>
  55. </td>
  56. </tr>
  57. </table>
  58. </td>
  59. </tr>""" % (LINE, ACCENT, FONT, FONT, INK, BRAND, FONT, MUTED)
  60. def _footer() -> str:
  61. url = site_url()
  62. return """
  63. <tr>
  64. <td style="padding:22px 40px 30px;border-top:1px solid %s;">
  65. <p style="margin:0 0 6px;font-size:12px;line-height:1.7;color:%s;">
  66. <strong style="color:%s;">%s</strong> · %s
  67. </p>
  68. <p style="margin:0;font-size:12px;line-height:1.7;color:%s;">
  69. © 2026 Steady Packaging&nbsp;&nbsp;|&nbsp;&nbsp;
  70. <a href="%s" style="color:%s;text-decoration:none;">%s</a>
  71. </p>
  72. </td>
  73. </tr>""" % (LINE, MUTED, INK, BRAND_FULL, TAGLINE, MUTED, url, MUTED,
  74. re.sub(r"^https?://", "", url))
  75. def _shell(inner: str, preheader: str = "") -> str:
  76. """整页外壳:暖纸底 + 居中卡片。"""
  77. return """<!doctype html>
  78. <html lang="zh-CN">
  79. <head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
  80. <title>%s</title></head>
  81. <body style="margin:0;padding:0;background-color:%s;">
  82. <div style="display:none;max-height:0;overflow:hidden;opacity:0;">%s</div>
  83. <table role="presentation" width="100%%" cellpadding="0" cellspacing="0" border="0" bgcolor="%s" style="background-color:%s;">
  84. <tr><td align="center" style="padding:32px 16px;">
  85. <table role="presentation" width="600" cellpadding="0" cellspacing="0" border="0"
  86. style="width:600px;max-width:600px;background-color:%s;border:1px solid %s;border-radius:16px;overflow:hidden;">
  87. %s
  88. %s
  89. %s
  90. </table>
  91. <p style="margin:16px 0 0;font-size:11px;color:%s;font-family:%s;">
  92. Steady Packaging 系统通知邮件
  93. </p>
  94. </td></tr>
  95. </table>
  96. </body></html>""" % (
  97. _esc(preheader), PAPER, _esc(preheader), PAPER, PAPER, CARD, LINE,
  98. _brand_header(), inner, _footer(), MUTED, FONT,
  99. )
  100. # ---------- 通用组件 ----------
  101. def _button(label: str, url: str) -> str:
  102. return """
  103. <tr><td style="padding:6px 0 4px;">
  104. <table role="presentation" cellpadding="0" cellspacing="0" border="0" align="center" style="margin:0 auto;">
  105. <tr><td align="center" bgcolor="%s" style="border-radius:10px;">
  106. <a href="%s" target="_blank"
  107. 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>
  108. </td></tr>
  109. </table>
  110. </td></tr>""" % (ACCENT, _esc(url), FONT, ACCENT, _esc(label))
  111. def _meta_rows(rows: list) -> str:
  112. """rows: [(label, value, value_strong_bool)],渲染成信息卡。"""
  113. items = []
  114. for label, value in rows:
  115. items.append("""
  116. <tr>
  117. <td style="padding:9px 0;font-size:13px;color:%s;white-space:nowrap;vertical-align:top;">%s</td>
  118. <td width="16"></td>
  119. <td align="right" style="padding:9px 0;font-size:13px;font-weight:600;color:%s;text-align:right;">%s</td>
  120. </tr>""" % (MUTED, _esc(label), INK, _esc(value)))
  121. return """
  122. <tr><td style="padding:20px 40px 4px;">
  123. <table role="presentation" width="100%%" cellpadding="0" cellspacing="0" border="0"
  124. style="background-color:%s;border:1px solid %s;border-radius:12px;">
  125. <tr><td style="padding:6px 18px;">
  126. <table role="presentation" width="100%%" cellpadding="0" cellspacing="0" border="0">
  127. %s
  128. </table>
  129. </td></tr>
  130. </table>
  131. </td></tr>""" % (ACCENT_SOFT, "#f3d8c8", "\n".join(items))
  132. # ---------- 场景一:登录验证码 ----------
  133. def verification_code(code: str, minutes: int = 10) -> tuple:
  134. """返回 (html, text)。"""
  135. inner = """
  136. <tr><td style="padding:34px 40px 8px;">
  137. <h1 style="margin:0 0 10px;font-family:%s;font-size:22px;font-weight:700;color:%s;">登录验证码</h1>
  138. <p style="margin:0 0 24px;font-size:15px;line-height:1.8;color:%s;">
  139. 你正在登录 <strong>Steady Packaging</strong>,请在验证码输入框填写以下 %d 位数字:
  140. </p>
  141. <table role="presentation" width="100%%" cellpadding="0" cellspacing="0" border="0">
  142. <tr><td align="center" bgcolor="%s"
  143. style="border:1px dashed %s;border-radius:12px;padding:22px 10px;">
  144. <span style="font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;
  145. font-size:36px;font-weight:700;letter-spacing:10px;color:%s;padding-left:10px;">%s</span>
  146. </td></tr>
  147. </table>
  148. <p style="margin:22px 0 0;font-size:13px;line-height:1.8;color:%s;">
  149. · 验证码 %d 分钟内有效,过期可重新获取<br>
  150. · 请勿将验证码转发给任何人,Steady 工作人员不会向你索取<br>
  151. · 如非本人操作,请忽略本邮件,你的账户安全不受影响
  152. </p>
  153. </td></tr>""" % (
  154. FONT, INK, INK, len(str(code)), ACCENT_SOFT, ACCENT, INK, _esc(code),
  155. MUTED, minutes,
  156. )
  157. html = _shell(inner, preheader="验证码 %s,%d 分钟内有效" % (code, minutes))
  158. text = (
  159. "【Steady】登录验证码\n\n"
  160. "你的登录验证码:%s(%d 分钟内有效)\n"
  161. "请勿将验证码转发给任何人。如非本人操作请忽略。\n\n"
  162. "Steady Packaging · %s" % (code, minutes, site_url())
  163. )
  164. return html, text
  165. # ---------- 场景二:订单沟通邮件(后台发给用户) ----------
  166. def order_message(subject: str, body: str, order: dict | None = None,
  167. title: str | None = None) -> tuple:
  168. """order 可含 order_no/summary/total/contact_name;返回 (html, text)。"""
  169. order = order or {}
  170. order_no = order.get("order_no") or ""
  171. if not title:
  172. title = re.sub(r"^【[^】]*】\s*", "", subject).strip() or "订单通知"
  173. rows = []
  174. if order_no:
  175. rows.append(("订单号", order_no))
  176. if order.get("total") not in (None, ""):
  177. try:
  178. rows.append(("订单金额", "¥%s" % format(float(order["total"]), ",.2f")))
  179. except (TypeError, ValueError):
  180. pass
  181. meta = _meta_rows(rows) if rows else ""
  182. cta = ""
  183. if order_no:
  184. cta = _button("查看我的订单", site_url() + "/orders.html")
  185. inner = """
  186. <tr><td style="padding:34px 40px 8px;">
  187. <h1 style="margin:0 0 18px;font-family:%s;font-size:21px;font-weight:700;color:%s;line-height:1.4;">%s</h1>
  188. %s
  189. </td></tr>
  190. %s
  191. %s
  192. <tr><td style="padding:18px 40px 0;">
  193. <p style="margin:0;font-size:13px;line-height:1.8;color:%s;">
  194. 你可以直接回复本邮件,或登录后在「我的订单」中查看进度。
  195. </p>
  196. </td></tr>""" % (
  197. FONT, INK, _esc(title), _nl2br(body), meta, cta, MUTED,
  198. )
  199. html = _shell(inner, preheader="订单 %s 有新的进展通知" % order_no)
  200. text_lines = ["【Steady】" + re.sub(r"^【[^】]*】\s*", "", subject).strip(), "", body.strip()]
  201. if rows:
  202. text_lines += ["", " ".join("%s:%s" % (k, v) for k, v in rows)]
  203. text_lines += ["", "查看订单:" + site_url() + "/orders.html", "", "Steady Packaging · " + site_url()]
  204. return html, "\n".join(text_lines)