使用 Resend 为代理设置专用邮箱,配置发送、通过 webhook 接收、收件箱存储及自动化监控。适用于以下情况:(1)...
代理电子邮件设置是一项面向实际任务的技能,主要用于给代理发送和接收的电子邮件地址—— 核查代码、通知、账户登记和自动收件箱监测;Prerequists.;
该技能适合需要稳定复用相关能力的场景,可作为自动化工作流的一部分,也便于后续检查、调整和扩展。从功能定位来看,该技能强调把分散的操作要求整理成清晰、可复用的处理流程,使用户能够围绕既定目标快速准备输入、选择执行方式并获得结构化结果。实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;
若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
为 Agent 分配独立邮箱地址,用于收发邮件——包括验证码、通知、账号注册以及自主邮箱收件箱监控。
curl、jq 和 node外部发件人
↓
[Resend 入站邮件]
↓ (Webhook 事件:email.received)
[Express /api/inbound-email]
↓ (以 JSON 格式保存)
[mail/inbox/]
↓ (定时任务:每 5 分钟执行一次)
[Agent 读取并处理]
↓
[Agent 响应或执行动作]
若该域名尚未验证,请执行以下操作:
RESEND_KEY="re_xxxxx"
# 添加域名
curl -s -X POST -H "Authorization: Bearer $RESEND_KEY"
-H "Content-Type: application/json"
https://api.resend.com/domains
-d '{"name": "yourdomain.com", "region": "us-east-1"}'
# 获取需配置的 DNS 记录
curl -s -H "Authorization: Bearer $RESEND_KEY"
https://api.resend.com/domains/yourdomain.com | jq '.records'
将返回的 DNS 记录(TXT、MX、CNAME)添加至您的 DNS 提供商控制台。等待验证完成(通常需数分钟)。
验证状态:
curl -s -H "Authorization: Bearer $RESEND_KEY"
https://api.resend.com/domains/yourdomain.com | jq '{status, capabilities}'
sending 与 receiving 两项能力均须显示为 "enabled"。
在 Express 后端中创建 routes/inboundEmail.js(完整实现参考 references/inboundEmail.js)。关键要求如下:
/api/inbound-email 的 POST 请求RESEND_WEBHOOK_SECRET 验证 Resend Webhook 签名mail/inbox/在服务端挂载该路由:
const inboundEmailRoutes = require('./routes/inboundEmail');
app.use('/api/inbound-email', inboundEmailRoutes);
curl -s -X POST -H "Authorization: Bearer $RESEND_KEY"
-H "Content-Type: application/json"
https://api.resend.com/webhooks
-d '{
"endpoint": "https://yourdomain.com/api/inbound-email",
"events": ["email.received"],
"enabled": true
}' | jq '{id, signing_secret}'
请妥善保存返回的 signing_secret —— 后续 Webhook 签名验证将用到它。
将以下配置项加入您的密钥管理系统:
{
"RESEND_API_KEY": "re_xxxxx",
"RESEND_WEBHOOK_SECRET": "whsec_xxxxx",
"AGENT_EMAIL": "agent@yourdomain.com"
}
mkdir -p mail/inbox touch mail/inbox/.lastcheck
设置一个每 5 分钟运行一次的 cron 任务,用于检查新邮件并通知 Agent:
每 5 分钟 → 检查 mail/inbox/ 下是否存在新的 .json 文件 → 若发现新邮件:读取、摘要,并向所有者报告 → 将已处理邮件重命名为带 .processed 后缀的文件
发送测试邮件:
curl -s -X POST -H "Authorization: Bearer $RESEND_KEY"
-H "Content-Type: application/json"
https://api.resend.com/emails
-d '{
"from": "Agent ",
"to": ["owner@theirdomain.com"],
"subject": "Email system test",
"text": "This is a test from my new email address."
}'
验证接收功能:
curl -s https://yourdomain.com/api/inbound-email | jq .
验证收件箱目录:
ls -la mail/inbox/
async function sendEmail(to, subject, text, html) {
const res = await fetch('https://api.resend.com/emails', {
method: 'POST',
headers: {
'Authorization': `Bearer ${RESEND_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
from: 'Agent ',
to: [to],
subject,
text,
...(html && { html }),
}),
});
return res.json();
}
async function getInbox() {
const res = await fetch('http://localhost:PORT/api/inbound-email');
const data = await res.json();
return data.emails;
}
async function getEmail(id) {
const res = await fetch(`http://localhost:PORT/api/inbound-email/${id}`);
return res.json();
}
async function ackEmail(id) {
await fetch(`http://localhost:PORT/api/inbound-email/${id}/ack`, { method: 'POST' });
}
Resend 使用 HMAC-SHA256 对 Webhook 进行签名。务必验证签名以防止伪造请求:
const crypto = require('crypto');
function verifySignature(rawBody, signature, secret) {
try {
const [timestamp, hash] = signature.split(',').map(s => s.trim());
if (!timestamp || !hash) return false;
const signedPayload = `${timestamp}.${rawBody}`;
const expected = crypto.createHmac('sha256', secret)
.update(signedPayload).digest('hex');
return crypto.timingSafeEqual(Buffer.from(hash), Buffer.from(expected));
} catch {
return false;
}
}
RESEND_API_KEY 或 RESEND_WEBHOOK_SECRET| 问题 | 解决方案 |
|---|---|
| 域名无法通过验证 | 检查 DNS 记录是否已生效(执行 dig TXT yourdomain.com) |
| Webhook 未触发 | 确认 Webhook 已启用(enabled),且端点地址使用 HTTPS |
| 签名校验失败 | 确保使用原始请求体(raw body),而非解析后的 JSON 对象 |
| 邮件未送达 | 检查域名的 receiving 能力是否已启用(enabled) |
| 后端未接收到请求 | 确认 Express 路由挂载顺序早于 body 解析中间件(如 express.json()) |
相关专题
热门下载
相关下载
精品课程
共6课时 | 54.7万人学习
共89课时 | 134.1万人学习
共49课时 | 82.6万人学习