Communitygithub.com

mailtrap-email-integration

指导智能体通过 Mailtrap 集成事务性邮件发送。

mailtrap-email-integration 是什么?

该技能为 AI 智能体提供了一套完整的集成方案,用于通过 Mailtrap 的 Email API 和沙箱功能向应用添加事务性邮件发送能力。它详细解释了沙箱环境与生产环境的分离原则——沙箱 API 会捕获邮件而不实际投递,适用于开发/测试环境,而生产环境则使用独立的认证域名端点。技能中包含了清晰的代码示例,展示如何在 TypeScript 中通过 Bearer Token 进行身份验证,并动态选择沙箱或生产端点。此外,还重点指出了常见反模式(如在非生产环境使用生产端点、硬编码令牌等),并提供了 DNS 域名验证、环境变量管理等最佳实践。它支持 Cursor、Codex 等平台,帮助开发者避免陷入邮件传递陷阱,确保集成的安全性和可靠性。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/affaan-m/everything-claude-code/tree/main/skills/mailtrap-email-integration

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

Mailtrap 邮件集成

使用 Mailtrap 的 Email API 和沙箱功能,为应用添加事务性邮件发送的模式,涵盖身份验证、环境隔离和常见投递陷阱。

何时激活

  • 实现“发送邮件”功能(注册确认、密码重置、通知、收据)
  • 调试开发/预发环境中邮件无法到达的问题
  • 为项目设置首次邮件发送集成
  • 审查直接调用邮件 API 而未进行沙箱隔离的代码

核心概念

沙箱与生产环境的隔离。 Mailtrap 提供沙箱 API,可捕获邮件而不实际投递,用于开发/预发环境,确保测试邮件不会进入真实收件箱。生产发送则使用独立的已验证域名端点。切勿将开发环境指向生产发送端点。

身份验证。 请求使用 Authorization 头部中的 Bearer 令牌。令牌按项目范围隔离;沙箱和生产环境通常使用不同的令牌。

域名验证。 生产发送要求通过 DNS 记录(SPF、DKIM、DMARC)验证发送域名,之后 Mailtrap 才会向真实收件人投递邮件。跳过此步骤可能导致静默投递失败或邮件进入垃圾邮件文件夹。

代码示例

// 通过 Mailtrap 的 Email API(生产环境)发送邮件
async function sendEmail(to: string, subject: string, html: string) {
  const response = await fetch("https://send.api.mailtrap.io/api/send", {
    method: "POST",
    headers: {
      "Authorization": `Bearer ${process.env.MAILTRAP_API_TOKEN}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      from: { email: "[email protected]", name: "你的应用" },
      to: [{ email: to }],
      subject,
      html,
    }),
  });

  if (!response.ok) {
    throw new Error(`邮件发送失败:${response.status}`);
  }
  return response.json();
}
// 同一调用,在非生产环境中路由到沙箱
const MAILTRAP_ENDPOINT = process.env.NODE_ENV === "production"
  ? "https://send.api.mailtrap.io/api/send"
  : `https://sandbox.api.mailtrap.io/api/send/${process.env.MAILTRAP_INBOX_ID}`;

反模式

反模式问题原因应采用的方案
在开发/测试环境中使用生产发送端点真实的测试邮件会到达真实收件箱,存在垃圾投诉风险和测试数据泄露风险将非生产环境路由到沙箱端点
在源码中硬编码 API 令牌一旦提交到版本控制,存在凭证泄露风险从环境变量/密钥管理器加载令牌
在域名验证完成前发送邮件静默失败或进入垃圾邮件箱在启用生产发送前验证 SPF/DKIM/DMARC 记录
发送失败时未设置重试/错误处理静默的通知失败(例如,用户从未收到密码重置邮件)检查响应状态,记录失败信息,展示可操作错误

最佳实践

  • 将沙箱和生产令牌保存在不同的环境变量中,切勿跨环境共享一个令牌
  • 在所有涉及邮件的生产上线前验证发送域名的 DNS 记录
  • 记录投递失败信息,并包含足够的调试上下文(收件人、模板、时间戳、响应码)
  • 将邮件发送视为可能失败的网络调用:使用 try/catch 包裹,切勿假设成功

相关技能

api-and-interface-design, security-and-hardening, ci-cd-and-automation

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

accessibility

Design, implement, and audit inclusive digital products using WCAG 2.2 Level AA. Use when building or auditing UI that must meet WCAG 2.2 Level AA, or when reviewing a change for keyboard, contrast, or screen-reader support.

affaan-m/claude-api

Anthropic Claude API patterns for Python and TypeScript. Covers Messages API, streaming, tool use, vision, extended thinking, batches, prompt caching, and Claude Agent SDK. Use when building applications with the Claude API or Anthropic SDKs.

affaan-m/everything-claude-code

End-to-end marketing campaign planning and execution. Covers audience research, positioning, campaign angle definition, landing page copy, email sequences, social posts, ad copy, short-form video scripts, and content calendars. Use as the orchestration layer for multi-channel product launches. Use when planning or executing a multi-channel product launch, or producing landing page, email, social, or ad copy.

affaan-m/everything-claude-code

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/everything-claude-code-conventions

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/frontend-design

Create distinctive, production-grade frontend interfaces with high design quality. Use when the user asks to build web components, pages, or applications and the visual direction matters as much as the code quality.

affaan-m/gget

gget CLI and Python workflow for quick genomic database queries, sequence lookup, BLAST-style searches, enrichment checks, and reproducible bioinformatics evidence logs.

affaan-m/literature-review

Systematic literature-review workflow for academic, biomedical, technical, and scientific topics, including search planning, source screening, synthesis, citation checks, and evidence logging.

affaan-m/motion-ui

Production-ready UI motion system for React/Next.js. Use when implementing animations, transitions, or motion patterns.

affaan-m/project-guidelines-example

Example project-specific skill template based on a real production application.

affaan-m/pubmed-database

Direct PubMed and NCBI E-utilities search workflows for biomedical literature, MeSH queries, PMID lookup, citation retrieval, and API-backed literature monitoring.

affaan-m/scholar-evaluation

Structured scholarly-work evaluation for papers, proposals, literature reviews, methods sections, evidence quality, citation support, and research-writing feedback.

affaan-m/uspto-database

USPTO patent and trademark data workflow for official record lookup, PatentSearch queries, TSDR checks, assignment data, and reproducible IP research logs.

agent-architecture-audit

Full-stack diagnostic for agent and LLM applications. Audits the 12-layer agent stack for wrapper regression, memory pollution, tool discipline failures, hidden repair loops, and rendering corruption. Produces severity-ranked findings with code-first fixes. Essential for developers building agent applications, autonomous loops, or any LLM-powered feature. Use when an agent or LLM feature misbehaves and the failing layer is unknown, or before shipping an agent stack.

agent-eval

Head-to-head comparison of coding agents (Claude Code, Aider, Codex, etc.) on custom tasks with pass rate, cost, time, and consistency metrics. Use when choosing between coding agents, or when a change to an agent setup needs measured pass rate, cost, and time rather than an impression.

agent-harness-construction

Design and optimize AI agent action spaces, tool definitions, and observation formatting for higher completion rates. Use when defining or revising an agent

agentic-engineering

Operate as an agentic engineer using eval-first execution, decomposition, and cost-aware model routing. Use when planning or executing engineering work that agents will carry out end to end.

agentic-os

Build persistent multi-agent operating systems on Claude Code. Covers kernel architecture, specialist agents, slash commands, file-based memory, scheduled automation, and state management without external databases. Use when building a persistent multi-agent system on Claude Code with its own memory, commands, and scheduling.

agent-introspection-debugging

Structured self-debugging workflow for AI agent failures using capture, diagnosis, contained recovery, and introspection reports. Use when an agent run fails and you need a reproducible diagnosis instead of a retry.

agent-payment-x402

Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol. Use when an agent must pay for something itself and needs per-task budgets, spending controls, and a non-custodial wallet.

相关技能