[Bug] Resend Webhook 收到未在数据库记录的邮件事件时抛出 500,导致 Resend 熔断关闭 Webhook
Author: taaneoCreated Sep 22, 2026Updated Sep 22, 2026
问题描述
在 resendService.webhooks 处理 Resend 回调时,如果收到的事件对应的 resendEmailId 在当前 D1 数据库中不存在(例如:用户在同一个 Resend 账号下配置了其他发信服务、外部自动化脚本或使用 Resend SMTP 发信),代码会执行以下逻辑:
// mail-worker/src/service/resend-service.js
const emailRow = await emailService.updateEmailStatus(c, params)
if (!emailRow) {
throw new BizError('更新邮件状态记录失败');
}随后在 mail-worker/src/api/resend-api.js 捕获并向 Resend 返回了 HTTP 500:
app.post('/webhooks', async (c) => {
try {
await resendService.webhooks(c, await c.req.json());
return c.text('success', 200)
} catch (e) {
return c.text(e.message, 500)
}
})造成的影响
- Resend 的 Webhook 是账号/Team 级别的全局事件管道,无法在 Resend 控制台按发信域名或项目过滤。该 Resend 账号下由其他服务发送的所有邮件(如自动通知、推送服务等),其送达事件同样会被推送到
/api/webhooks。 - 当收到这些非本系统生成的邮件时,接口持续返回 HTTP 500,Resend 在重试 8 次失败后会判定该 Webhook 接收端异常,自动将 Webhook 状态置为 Disabled(关闭)。
- 最终导致 Cloud Mail 自身原本正常发送的邮件也无法再接收到送达(Delivered)与弹回(Bounced)状态更新。
建议修复方案
按照 Webhook 接收端的标准规范:对于不属于本系统或在数据库中找不到记录的事件,应该视为正常交付并静默忽略,返回 HTTP 200,避免上游认为服务异常而重试或熔断。
建议修改 mail-worker/src/service/resend-service.js:
const emailRow = await emailService.updateEmailStatus(c, params)
if (!emailRow) {
// 外部其他服务发出的邮件在当前数据库无记录,直接忽略,不抛出异常,返回 200
console.warn(`[Webhook] 未在数据库中找到邮件记录(${params.resendEmailId}),忽略处理`);
return;
}Source: maillab/cloud-mail