Transactional email template builder

From findscripter/everything-skills · View interactive version

Transactional email template builder is an AI agent skill from the GitHub repository findscripter/everything-skills. 当为新产品搭建事务邮件、迁移邮件服务商或重构遗留邮件模板时使用;用 React Email 组件 + 统一发送层产出含暗黑模式/i18n/防垃圾优化/UTM 追踪的生产级模板系统;不适用于纯营销群发活动、富交互 HTML 页面或站内信。触发词:事务邮件、邮件模板、React Email、Resend、邮件发送、邮件可达性

How to use this skill

Skill file

何时使用

适用:

不该用(负边界):

步骤

  1. 按结构落地目录:components/(layout、partials)、templates/(各类邮件)、lib/(send.ts 统一发送、providers/、tracking.ts)、i18n/(en.ts、de.ts…)、preview/。
  2. 先写基础布局 EmailLayout:品牌头/脚、<Preview> 文案、暗黑模式 @media (prefers-color-scheme: dark)(覆盖内联样式必须加 !important)、退订与隐私链接。
  3. 基于布局写具体模板(如 WelcomeEmail、InvoiceEmail),props 用 TypeScript 接口约束,CTA 用 <Button>,金额用 Intl.NumberFormat 格式化。
  4. 实现统一发送函数 sendEmail(to, payload):用判别联合类型映射模板与主题,render() 成 HTML,过 addTrackingParams 注入 UTM,再调服务商 SDK 发送并打 tags。
  5. 接入 i18n:按 locale 选择翻译表(locale === "de" ? de : en),翻译值用函数支持插值。
  6. 起本地预览服务热重载校验,发前对照防垃圾清单并用 Mail-Tester 自测(目标 9+/10)。

指令

npm run email:dev    # email dev --dir emails/templates --port 3001  → http://localhost:3001 实时预览
npm run email:build  # email export --dir emails/templates --outDir emails/out

package.json scripts:

{
  "scripts": {
    "email:dev": "email dev --dir emails/templates --port 3001",
    "email:build": "email export --dir emails/templates --outDir emails/out"
  }
}

示例

统一发送层(emails/lib/send.ts,判别联合 + UTM + tags):

import { Resend } from "resend"
import { render } from "@react-email/render"
import { WelcomeEmail } from "../templates/welcome"
import { InvoiceEmail } from "../templates/invoice"
import { addTrackingParams } from "./tracking"

const resend = new Resend(process.env.RESEND_API_KEY)

type EmailPayload =
  | { type: "welcome"; props: Parameters<typeof WelcomeEmail>[0] }
  | { type: "invoice"; props: Parameters<typeof InvoiceEmail>[0] }

export async function sendEmail(to: string, payload: EmailPayload) {
  const templates = {
    welcome: { component: WelcomeEmail, subject: "Welcome to MyApp — confirm your email" },
    invoice: { component: InvoiceEmail, subject: "Invoice from MyApp" },
  }
  const template = templates[payload.type]
  const html = render(template.component(payload.props as any))
  const trackedHtml = addTrackingParams(html, { campaign: payload.type })

  return resend.emails.send({
    from: "MyApp <hello@yourapp.com>",
    to,
    subject: template.subject,
    html: trackedHtml,
    tags: [{ name: "email-type", value: payload.type }],
  })
}

UTM 追踪(emails/lib/tracking.ts,给所有链接追加参数):

export function addTrackingParams(html: string, params: { campaign: string; medium?: string; source?: string }): string {
  const utm = new URLSearchParams({
    utm_source: params.source ?? "email",
    utm_medium: params.medium ?? "transactional",
    utm_campaign: params.campaign,
  }).toString()
  return html.replace(/href="(https?:\/\/[^"]+)"/g, (m, url) => {
    const sep = url.includes("?") ? "&" : "?"
    return `href="${url}${sep}${utm}"`
  })
}

暗黑模式(写在布局 <Head> 内,必须 !important 覆盖内联样式):

@media (prefers-color-scheme: dark) {
  .email-body { background-color: #0f0f0f !important; }
  .email-container { background-color: #1a1a1a !important; }
  .email-text { color: #e5e5e5 !important; }
}

注意事项

防垃圾 / 可达性清单(发前逐项核对):

常见坑:

互见


采编自 alirezarezvani/claude-skills(MIT)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.