如何通过 webhook 接收收到的邮件
每两秒轮询一次邮箱也能用,但 webhook 更快也更安静:邮件一到,QuickInbox 就调用你的 URL。
设置 webhook
Webhook 按命名空间设置,可以在工作区中设置,也可以用 API:
curl -X PATCH "https://business.quickinbox.net/api/business/namespaces/1/webhook" \
-H "X-API-Key: $QI_KEY" -H "content-type: application/json" \
-d '{"webhookUrl": "https://ci.example.com/hooks/mail"}'每个套餐都包含 webhook,免费套餐也有。准确的请求体请查看 API 文档。
负载
{
"event": "mail.received",
"namespace": "acme7x",
"message": {
"id": "b2c3d4e5-...",
"to": "acme7x.signup-test",
"from": "sender@example.com",
"subject": "Confirm your signup",
"text": "...",
"html": "...",
"spam": { "score": 100, "spf": "pass", "dkim": "pass", "dmarc": "pass" },
"receivedAt": 1893455460
}
}to 字段是不带域名的 namespace.tag,所以你可以把它和测试使用的标签对应起来。
一个小型接收端(Node)
import http from 'node:http';
const waiting = new Map(); // tag -> resolve function
export function waitForWebhookEmail(tag, timeoutMs = 60_000) {
return new Promise((resolve, reject) => {
waiting.set(tag, resolve);
setTimeout(() => reject(new Error(`No webhook for ${tag}`)), timeoutMs);
});
}
http.createServer((req, res) => {
let body = '';
req.on('data', (c) => (body += c));
req.on('end', () => {
const { message } = JSON.parse(body);
const tag = message.to.split('.').slice(1).join('.');
waiting.get(tag)?.(message);
res.end('ok');
});
}).listen(8787);保留后备方案
投递失败的 webhook 不会重试。如果需要保证,超时后轮询 GET /api/business/mail,或使用实时流接口 GET /api/business/mail/stream。把 webhook 当作快速通道,把 API 当作安全网。
在本地测试?
你的笔记本无法从互联网访问。可以用隧道工具,或者干脆在本地轮询 API,在 CI 中使用 webhook。
相关指南
- 邮件测试工具对比:QuickInbox、Mailtrap、Mailosaur、Mailinator、MailSlurp、testmail
- 如何用 Python 在 Selenium 中测试邮件验证
- 在 Jest 和 Node.js 中做邮件测试
- 用 pytest 在 Python 中做邮件测试
- 邮件测试 API 概览
获取私有邮箱:QuickInbox Business API 提供每月 5,000 封邮件的免费套餐,付费套餐每月 $9 起。创建免费账户或阅读 API 文档。