Send and receive emails with Mailgun. Use when a user asks to send bulk emails, set up email routing, validate email addresses, track email engagement, or handle both transactional and marketing emails.
Mailgun is a developer-focused email API for sending, receiving, and tracking emails. It handles both transactional and bulk marketing emails, includes email validation, and provides detailed analytics (opens, clicks, bounces, unsubscribes).
// lib/mailgun.ts — Mailgun client
import Mailgun from 'mailgun.js'
import formData from 'form-data'
const mg = new Mailgun(formData).client({
username: 'api',
key: process.env.MAILGUN_API_KEY!,
url: 'https://api.eu.mailgun.net', // use EU endpoint for GDPR
})
// Simple send
await mg.messages.create('mg.myapp.com', {
from: 'MyApp <noreply@mg.myapp.com>',
to: ['user@example.com'],
subject: 'Your weekly report',
html: '<h1>Weekly Report</h1><p>Here are your stats...</p>',
text: 'Weekly Report\n\nHere are your stats...',
'o:tag': ['weekly-report'],
'o:tracking-clicks': 'yes',
'o:tracking-opens': 'yes',
})
// Validate before sending (prevents bounces)
const validation = await mg.validate.get('user@example.com')
console.log(validation.result) // "deliverable", "undeliverable", "risky"
console.log(validation.risk) // "low", "medium", "high"
if (validation.result === 'undeliverable') {
console.log('Bad email, skipping')
}
// routes/webhooks/mailgun.ts — Track email events
import crypto from 'crypto'
function verifyMailgunWebhook(token: string, timestamp: string, signature: string): boolean {
const hash = crypto.createHmac('sha256', process.env.MAILGUN_WEBHOOK_KEY!)
.update(timestamp + token)
.digest('hex')
return hash === signature
}
export async function handleMailgunWebhook(req) {
const { signature, 'event-data': eventData } = req.body
if (!verifyMailgunWebhook(signature.token, signature.timestamp, signature.signature)) {
return { status: 401 }
}
switch (eventData.event) {
case 'delivered':
await markEmailDelivered(eventData.message.headers['message-id'])
break
case 'opened':
await trackEmailOpen(eventData.recipient, eventData.timestamp)
break
case 'clicked':
await trackEmailClick(eventData.recipient, eventData.url)
break
case 'failed':
if (eventData.severity === 'permanent') {
await removeBouncedEmail(eventData.recipient)
}
break
case 'complained':
await unsubscribeUser(eventData.recipient)
break
}
}
api.eu.mailgun.net) stores data in EU for GDPR compliance.npx skills add TerminalSkills/mailgun下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer