详细的Mailgun API使用说明,包含完整的邮件发送示例和最佳实践
Mailgun是一个强大的邮件发送服务,提供可靠的邮件投递、跟踪和分析功能。本指南将帮助您快速掌握Mailgun API的使用方法。
开始使用前,您需要准备以下信息:
最基本的邮件发送示例。
curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN/messages \
-F from='Support ' \
-F to='user@example.com' \
-F subject='Hello' \
-F text='Testing some Mailgun awesomeness!'
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| from | string | 是 | 发件人地址(格式:Name <email@domain>) |
| to | string | 是 | 收件人地址(可多个,逗号分隔) |
| subject | string | 是 | 邮件主题 |
| text | string | 否 | 纯文本内容 |
| html | string | 否 | HTML格式内容 |
{
"id": "<20111114174239.25659.5817@samples.mailgun.org>",
"message": "Queued. Thank you."
}
向多个收件人发送邮件,支持最多1000个收件人。
curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN/messages \
-F from='Marketing ' \
-F to='user1@example.com,user2@example.com,user3@example.com' \
-F subject='Summer Sale Announcement' \
-F text='Check out our amazing deals!'
为每个收件人定制邮件内容,实现个性化批量发送。
curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN/messages \
-F from='Support ' \
-F to='user1@example.com,user2@example.com' \
-F subject='Hello %recipient.name%' \
-F text='Hi %recipient.name%, your code is %recipient.code%' \
-F recipient-variables='{
"user1@example.com": {"name": "Alice", "code": "A123"},
"user2@example.com": {"name": "Bob", "code": "B456"}
}'
%recipient.变量名% 格式user1@example.com 收到: 主题:Hello Alice 内容:Hi Alice, your code is A123 user2@example.com 收到: 主题:Hello Bob 内容:Hi Bob, your code is B456
发送富文本HTML格式的邮件,提升用户体验。
curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN/messages \
-F from='Marketing ' \
-F to='user@example.com' \
-F subject='Welcome to Our Service' \
-F text='Plain text version for email clients that dont support HTML' \
-F html='<html>
<body style="font-family: Arial, sans-serif;">
<h1 style="color: #E94F37;">Welcome!</h1>
<p>Thank you for joining our service.</p>
<a href="https://example.com" style="background: #E94F37; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;">Get Started</a>
</body>
</html>'
在邮件中添加文件附件。
curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN/messages \
-F from='Support ' \
-F to='user@example.com' \
-F subject='Your Invoice' \
-F text='Please find your invoice attached.' \
-F attachment=@/path/to/invoice.pdf \
-F attachment=@/path/to/receipt.pdf
启用邮件打开和点击追踪功能。
curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN/messages \
-F from='Marketing ' \
-F to='user@example.com' \
-F subject='Product Update' \
-F html='<html><body><a href="https://example.com">Check it out</a></body></html>' \
-F o:tracking=yes \
-F o:tracking-clicks=yes \
-F o:tracking-opens=yes
| 参数 | 说明 |
|---|---|
| o:tracking | 启用/禁用追踪(yes/no) |
| o:tracking-clicks | 追踪链接点击(yes/no/htmlonly) |
| o:tracking-opens | 追踪邮件打开(yes/no) |
A: 请检查:
A: 可能的原因:
A: 限制说明:
A: 建议步骤: