如何在 sendgrid v3 node.js 中向多个收件人发送电子邮件

Tru*_*ran 4 email node.js sendgrid

有人可以帮我在 sendgrid v3 + node.js 中向多个收件人发送电子邮件吗?我注意到当我在to字段中输入多个电子邮件地址时,只有第一个电子邮件地址会收到电子邮件。第一个之后的电子邮件地址没有收到电子邮件:

send: function(email, callback) {
    var from_email = new helper.Email(email.from);
    var to_email = new helper.Email('emailUser1@gmail.com,emailUser2@gmail.com,emailUser3@gmail.com');
    var subject = email.subject;
    var content = email.content
    var mail = new helper.Mail(from_email, subject, to_email, content);
    var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
    var request = sg.emptyRequest({
      method: 'POST',
      path: '/v3/mail/send',
      body: mail.toJSON(),
    });

    sg.API(request, function(err, res) {
        console.log(res);
        if(err) {
            console.log('---error sending email:---');
            console.log(err);
            console.log(err.response.body);
            callback(500);
        } else {
            callback(200);
        }
    });

}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,只emailUser1@gmail.com接收电子邮件;emailUser2@gmail.com并且emailUser3@gmail.com没有收到电子邮件。

有人可以帮忙吗?

提前致谢!

vim*_*mmi 5

const sgMail = require('@sendgrid/mail');
module.exports.send = function () {
    sgMail.setApiKey('XYZ');
    const msg = {
        to: ['abc@gmal.com', 'xyz@gmail.com'],
        cc: ['test@gmail.com', 'testing@gmail.com'],
        from: 'no-reply@mail.total.fr',
        subject: 'Subject of mail',
        html: 'html body',
        text: 'text message'
    };
    // console.log('message in mail :: ', msg);
    sgMail.send(msg).catch(console.error);
};
Run Code Online (Sandbox Code Playgroud)


Tre*_*eaf 5

node js 邮件助手允许您通过将to属性指定为数组来发送给多个收件人。然后根据您是否希望收件人能够看到彼此的地址,您以稍微不同的方式发送邮件:

要允许查看,请使用sgMail.send(msg)

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: ['recipient1@example.org', 'recipient2@example.org'],
  from: 'sender@example.org',
  subject: 'Hello world',
  text: 'Hello plain world!',
  html: '<p>Hello HTML world!</p>',
};
sgMail.send(msg);
Run Code Online (Sandbox Code Playgroud)

为防止看到、使用sgMail.sendMultiple(msg)sgMail.send(msg, true)

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: ['recipient1@example.org', 'recipient2@example.org'],
  from: 'sender@example.org',
  subject: 'Hello world',
  text: 'Hello plain world!',
  html: '<p>Hello HTML world!</p>',
};
sgMail.sendMultiple(msg);
Run Code Online (Sandbox Code Playgroud)

https://github.com/sendgrid/sendgrid-nodejs/blob/b3b2092b7a150ffc5d68c9bb6575810a5827f69e/docs/use-cases/single-email-multiple-recipients.md

在幕后Personalizations,您可以使用帮助程序进行更好的控制:

https://sendgrid.com/docs/for-developers/sending-email/personalizations/


jac*_*fwd 1

您正在使用 SendGrid 的帮助程序库吗?您将需要利用个性化

如果您希望收件人看到彼此,您应该在单个个性化对象中命名并填充每个收件人。如果您不希望他们看到彼此,并希望他们各自单独接收消息,则需要为每个不同的收件人组创建一个新的个性化对象。

  • 正确的。为了防止有人无意中无意中向所有人发送“TO”,SendGrid 不会以这种方式构建本机 To: 标头。他们希望您使用个性化对象,以便明确您的收件人交叉可见性意图。 (2认同)