如何通过SendGrid API单请求批量发送邮件?
SendGrid 单次请求发送多封邮件的API端点说明
SendGrid的/mail/send端点其实是支持单次请求发送多封邮件的,你之前可能是请求格式不对。
具体用法
直接向/mail/send端点发送一个包含多个邮件对象的数组即可,逻辑和你用sendMultiple(messages)完全一致。
举个Node.js原生请求的示例(用axios):
const axios = require('axios'); // 构造多封邮件的数组 const batchMessages = [ { to: 'user1@example.com', from: 'your-sender@example.com', subject: '批量测试邮件1', text: '这是第一封批量发送的邮件内容' }, { to: 'user2@example.com', from: 'your-sender@example.com', subject: '批量测试邮件2', text: '这是第二封批量发送的邮件内容' } ]; // 发起批量请求 axios.post('https://api.sendgrid.com/v3/mail/send', batchMessages, { headers: { 'Authorization': 'Bearer 你的SendGrid API密钥', 'Content-Type': 'application/json' } }) .then(res => console.log('批量发送完成')) .catch(err => console.error('发送出错:', err.response.data));
注意事项
- 每个邮件对象必须符合SendGrid的格式要求,必填字段包括
to、from、subject,以及text或html内容字段。 - 单次请求最多支持发送1000封邮件,超过这个数量需要拆分多个请求。
- 你用的
sendMultiple方法本质就是封装了这个批量请求逻辑,底层调用的就是/mail/send端点。
内容的提问来源于stack exchange,提问作者Tholinhlanhla
相关产品推荐
相关产品推荐

