Node.js使用SendGrid发送营销邮件时如何隐藏to字段仅保留BCC收件人
SendGrid 接口要求邮件配置必须携带to参数,直接删除会触发参数校验错误,无法发送邮件,你可以通过以下两种方案实现需求:
- 方案1:将
to参数值设置为和from一致的发件邮箱,收件人收到邮件时,to字段会展示发件人地址,不会出现你配置的测试邮箱,同时满足接口参数要求。 - 方案2:将
to参数值设置为邮件协议约定的隐藏收件人标识'undisclosed-recipients:;',收件人端会显示「未公开收件人」类的提示,完全不暴露任何收件人地址,更适配营销邮件的群发场景。
修改后可直接运行的参考代码:
require("dotenv").config(); const sgMail = require('@sendgrid/mail') const fs = require("fs"); sgMail.setApiKey(process.env.SENDGRID_API_KEY) const msg = { to: 'undisclosed-recipients:;', // 也可替换为和from一致的发件人邮箱 from: 'exemple1@gmail.com', bcc: ['exemple2@gmail.com','exemple3@gmail.com','exemple4@gmail.com'], subject: 'Sending Emails Using Sendgrid', html: '<p>Sending Emails Using Sendgrid</p><br><img src="cid:logo" alt="image" />', attachments: [{ filename: 'img', type: 'image/png', content_id: 'logo', content: fs.readFileSync('imgs/img.png', { encoding: 'base64' }), disposition: 'inline', }], }; sgMail.send(msg).then(() => { console.log('Email sent') }).catch((error) => { console.error(error) });
内容的提问来源于stack exchange,提问作者AMINE DAIEF
相关产品推荐
相关产品推荐

