resend-cli 是做什么的?
通过终端操作 Resend 平台 — 发送邮件(包括 React Email .tsx 模板,通过 --react-email 参数)、管理域名、联系人、广播、模板、Webhook、API 密钥、日志、自动化和事件,均通过 resend CLI 实现。当用户希望在 Shell、脚本或 CI/CD 管道中运行 Resend 命令,或发送/预览 React Email 模板时使用此技能。在执行 resend 命令前务必加载此技能 — 内含非交互式标志契约和防止静默失败的注意事项。
可用命令
所有命令均支持使用 --json 和 --no-color 标志以支持非交互式使用。
邮件
resend emails send --from "" --to "" --subject "" --text ""— 发送纯文本邮件resend emails send --from "" --to "" --subject "" --react-email /absolute/path/to/template.tsx— 发送 React Email 模板。必须提供--from、--to、--subject和--react-email。路径必须是绝对路径。resend emails send --from "" --to "" --subject "" --html "<h1>Hello</h1>"— 发送 HTML 内容resend emails send --help— 查看所有发送选项resend emails list— 列出已发送邮件resend emails get <id>— 获取邮件详情resend emails cancel <id>— 取消计划发送的邮件
域名
resend domains list— 列出所有域名resend domains add <name> --region us|eu— 添加域名resend domains verify <id>— 触发验证resend domains get <id>— 获取域名详情
受众
resend audiences list— 列出所有受众resend audiences add <name>— 创建新受众resend audiences get <id>— 获取受众详情resend audiences remove <id>— 删除受众
联系人
resend contacts list --audience-id <id>— 列出受众下的联系人(需要--audience-id)resend contacts create --email [email protected] --audience-id <id>— 向受众添加联系人(需要--audience-id,可选--first-name、--last-name、--unsubscribed)resend contacts get <id>— 获取联系人详情resend contacts update <id> --unsubscribed true|false— 更新联系人订阅状态resend contacts remove <id>— 删除联系人
广播
resend broadcasts list— 列出所有广播resend broadcasts send --audience-id <id> --from "" --subject ""— 发送广播。需要先创建受众并添加联系人。支持--react-email、--html、--text等参数。resend broadcasts get <id>— 获取广播详情
模板
resend templates list— 列出所有模板resend templates add --name "Name" --subject "Subject" --html "<html>...</html>"— 创建模板resend templates get <id>— 获取模板详情resend templates update <id> --name "New Name"— 更新模板resend templates remove <id>— 删除模板
Webhook
resend webhooks list— 列出所有 Webhookresend webhooks add --url https://example.com/hook --events email.sent,email.delivered— 创建 Webhookresend webhooks get <id>— 获取 Webhook 详情resend webhooks update <id> --url https://new-url.com/hook— 更新 Webhookresend webhooks remove <id>— 删除 Webhook
API 密钥
resend api-keys list— 列出所有 API 密钥resend api-keys add --name "My Key" --permission full|sending— 创建 API 密钥(返回密钥值)resend api-keys remove <id>— 删除 API 密钥
日志
resend logs list— 列出日志事件(支持--type过滤,如email.sent、email.bounced)
自动化
resend automations list— 列出所有自动化规则resend automations create --name "Welcome" --trigger created --action send-email --audience-id <id>— 创建自动化规则resend automations get <id>— 获取自动化详情resend automations remove <id>— 删除自动化规则
事件
resend events list— 列出所有可用事件resend events get <id>— 获取事件详情
非交互式标志
所有命令均支持以下标志以支持在脚本和 CI/CD 中使用:
--json— 以 JSON 格式输出结果--no-color— 禁用彩色输出
使用场景
- 在 CI/CD(GitHub Actions、GitLab CI)中发送事务性邮件 — 使用
resend emails send配合--json标志 - 管理邮件基础设施 — 添加并验证域名,创建受众并管理联系人
- 发送 React Email 模板预览 — 在开发或 CI 环境中使用
--react-email标志 - 通过广播进行营销活动 — 先创建受众,添加联系人,然后发送广播
- 设置自动化工作流 — 定义基于事件的自动化规则
- 监控邮件传递状态 — 查询日志和事件以跟踪发送状态
静默失败注意事项
- 广播发送失败 — 发送广播前,确认受众已存在且包含至少一个未退订的联系人。如果受众为空,广播会静默失败。
- React Email 模板路径 — 必须使用绝对路径。相对路径会导致静默失败。
- 联系人订阅状态 — 创建联系人时默认未退订。如果
--unsubscribed设为true,广播将不会发送给他们。 - API 密钥权限 — 创建 API 密钥时,密钥值仅在创建时显示一次。如果丢失,需重新创建。
- 域名验证 — 域名添加后需验证。未验证的域名无法发送邮件,但命令仍会返回成功。
环境要求
- 需要 Node.js 16 或更高版本
- 通过 npm 全局安装:
npm install -g resend-cli - 需要有效的 Resend API 密钥(可通过
resend api-keys add创建) - 通过
RESEND_API_KEY环境变量或resend login配置
示例
# 发送简单邮件
resend emails send --from "[email protected]" --to "[email protected]" --subject "Hello" --text "Welcome!"
# 发送 React Email 模板
resend emails send --from "[email protected]" --to "[email protected]" --subject "Hello" --react-email /home/user/emails/welcome.tsx --json
# 创建受众并添加联系人
resend audiences add "Newsletter Subscribers"
resend contacts create --email [email protected] --audience-id <audience_id>
# 发送广播
resend broadcasts send --audience-id <audience_id> --from "[email protected]" --subject "Monthly Update" --html "<h1>Hello</h1>"
# 使用 JSON 输出创建 API 密钥(用于脚本)
resend api-keys add --name "CI Key" --permission sending --json
故障排除
如果遇到问题:
- 验证
RESEND_API_KEY环境变量已设置 - 检查 API 密钥权限是否足够(发送邮件需要
sending权限) - 确认域名已验证
- 对 React Email 模板使用绝对路径
- 在脚本中使用
--json标志以更好地处理错误 - 查看日志:
resend logs list --type email.bounced