Communitygithub.com

mailtrap-email-integration

引導 AI 智能體透過 Mailtrap 整合應用程式的交易型電子郵件發送功能。

mailtrap-email-integration 是什麼?

這個技能教導 AI 智能體如何將 Mailtrap 的交易型電子郵件發送功能整合到應用程式中。它會引導智能體完成整個設定流程,包括認證、沙箱與生產環境分離,以及常見的遞送問題排解。此技能對於正在實作「寄送電子郵件」功能(例如註冊確認、密碼重設、通知或收據)的開發人員特別有用,也適用於除錯開發或測試環境中電子郵件無法送達的情況。此技能支援 Node.js / TypeScript 程式碼範例,並涵蓋 Mailtrap 沙箱 API(用於開發/測試)和生產發送 API。關鍵觀念包括使用 Bearer Token 進行認證、透過 DNS 記錄驗證寄件網域,以及避免在非生產環境使用生產環境端點。它也列舉了常見的反模式(例如在程式碼中寫死 API Token),並強調錯誤處理和重試機制。

相容平台~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 而沒有使用沙箱隔離的程式碼

核心概念

沙箱 vs. 生產環境分離。 Mailtrap 提供一個沙箱 API,會攔截電子郵件而不實際遞送,用於開發/測試環境,這樣測試郵件就不會送達真實的收件匣。生產環境發送則使用另一個經過網域驗證的端點。絕對不要將開發環境指向生產環境的發送端點。

認證。 請求需要在 Authorization 標頭中使用 Bearer Token。Token 的作用域是針對每個專案;沙箱和生產環境通常使用不同的 Token。

網域驗證。 生產環境發送需要事先透過 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: "Your App" },
      to: [{ email: to }],
      subject,
      html,
    }),
  });

  if (!response.ok) {
    throw new Error(`Email send failed: ${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 Token如果提交到版本控制,有憑證外洩的風險從環境變數 / 密碼管理員載入 Token
在網域驗證完成前就開始發送郵件會靜默失敗或進入垃圾郵件在啟用生產環境發送前,先驗證 SPF/DKIM/DMARC 記錄
發送失敗時沒有重試/錯誤處理靜默的通知失敗(例如使用者永遠收不到密碼重設郵件)檢查回應狀態碼、記錄失敗、提供可操作的錯誤訊息

最佳實踐

  • 將沙箱和生產環境的 Token 放在不同的環境變數中,絕對不要讓一個 Token 跨環境使用
  • 在任何涉及電子郵件的生產環境發布前,先驗證寄件網域的 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.

相關技能