SendGrid 是按发送量计算全球最大的邮件 API——由 Twilio 收购,Uber、Airbnb、Spotify 等企业均在使用。专为大规模场景设计:支持子账户、专用 IP、IP 预热,以及营销邮件与事务邮件一体化管理。代价是界面相对复杂,中等发送量的套餐定价也偏高。
前往 signup.sendgrid.com ↗ 注册。免费套餐在 60 天试用期结束后可永久保留每日 100 封的发送配额;付费套餐从 $19.95/月起,每月可发 5 万封。
SendGrid 的注册流程包含账号审核环节——为了防止滥用,他们有时会对新账号进行额外确认。请耐心等待。
两种验证方式:
进行域名身份验证:Settings → Sender Authentication → Authenticate Your Domain。选择你的 DNS 服务商,SendGrid 会生成 3 条 CNAME 记录:
em1234.yourdomain.com → SendGrid 邮件服务器s1._domainkey.yourdomain.com → DKIM 记录 1s2._domainkey.yourdomain.com → DKIM 记录 2将这些记录添加到你的 DNS 服务商,然后回到 SendGrid 点击 Verify。通常 10–30 分钟内即可通过验证。
Settings → API Keys → Create API Key,配置以下选项:
复制生成的 Key(以 SG. 开头),它只会显示一次。将其存入环境变量:
SENDGRID_API_KEY=SG.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
npm install @sendgrid/mail
import sgMail from "@sendgrid/mail";
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
await sgMail.send({
to: "[email protected]",
from: "[email protected]", // must be a verified sender
subject: "Hello from SendGrid",
text: "Plain text version",
html: "<p>HTML body</p>",
});
检查收件箱,如果没有收到,可以在 SendGrid 控制台的 Activity 中查看——每次发送都会记录投递状态以及收件方邮件服务器的响应信息。
对于需要重复发送的邮件(欢迎信、收据、密码重置等),可以使用 动态模板(Dynamic Templates)——在 SendGrid 控制台设计好后,通过模板 ID 调用发送。
Email API → Dynamic Templates → Create Template。可以使用拖拽编辑器或 HTML 编辑器。变量使用 Handlebars 语法,格式为 {{name}}。
通过模板 ID 发送:
await sgMail.send({
to: user.email,
from: "[email protected]",
templateId: "d-xxxxxxxxxxxxxxxxxxxxxxxx", // starts with d-
dynamicTemplateData: {
name: user.name,
verifyUrl: `https://yourapp.com/verify/${token}`,
},
});
在控制台修改模板后,所有后续发送将立即生效——无需重新部署。
Settings → Mail Settings → Event Webhook。填入你的接口地址,选择需要监听的事件:已投递、已打开、已点击、退信、被丢弃、被举报为垃圾邮件。
SendGrid 会批量 POST 事件数据到你的接口。建议开启签名事件 Webhook 以确保安全——SendGrid 会对请求体进行签名,你在服务端验签即可。签名 Webhook 文档 ↗。
如何处理这些事件:
免费及入门套餐默认使用共享 IP。发送量较大时可申请专用 IP——但新 IP 没有发送信誉,需要在 4–6 周内逐步提高发送量来完成"预热"。
SendGrid 支持自动化预热:在账号设置中开启 IP 预热功能,SendGrid 会按照逐步递增的每日限额来控制你的发送量。跳过预热直接大量发送,邮件大概率会进入垃圾邮件箱。
IP 预热计划 ↗。每月发送量低于约 10 万封时,共享 IP 池完全够用。
SendGrid 的 Subusers(子用户) 功能允许一个 SendGrid 主账号下管理多个独立的子账号,每个子账号拥有各自的 API Key、发件人身份验证和 IP 池。适合旗下有多个产品线或运营多租户平台的场景。
Settings → Subuser Management。创建子用户并设置独立凭据,分配 IP 池和发送限额。子用户文档 ↗。
大多数独立开发者和 SaaS 产品用不到这个功能。如果你的规模还不需要多账户体系,选其他服务商会更合适。
免费套餐:每日 100 封(60 天 4 万封/月试用期结束后永久有效)。Essentials:$19.95/月,可发 5 万封。Pro:$89.95/月,可发 10 万封,含专用 IP。SendGrid 定价 ↗。
大规模发送时单封成本有竞争力(百万封/月级别约 $0.0004/封)。每月发送量低于 5 万封时,Resend 在相近开发体验下更便宜。