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

11

有人能帮我在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);
        }
    });

}

在上面的例子中,只有emailUser1@gmail.com收到了邮件;emailUser2@gmail.comemailUser3@gmail.com没有收到邮件。
有人能帮忙吗?
提前致谢!

5个回答

18

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);

为了避免被查看,请使用 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);

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/


9
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);
};

2
{
  "from": "sender@yourdomain.com",
  "template_id": "YOUR TEMPLATE ID",
  "personalizations": [
    {
      "to": [
        {
          "email": "john@example.com"
        }
      ],
      "send_at": 1600188812
    },
    {
      "to": [
        {
          "email": "jane@example.com"
        }
      ],
      "send_at": 1600275471
    }
  ]
}

1
你是否在使用SendGrid的助手库?你需要利用个性化功能。
如果你希望收件人看到彼此,你应该在单个个性化对象中命名和填充每个收件人。如果你不希望他们看到彼此,并希望每个收件人分别接收消息,则需要为每个不同的收件人组创建一个新的个性化对象。

是的,我正在使用SendGrid的辅助库 - 所以您的意思是将每个人的电子邮件列在单个字符串中不起作用吗?例如:to: email1@gmail.com; email2@gmail.com; email3@gmail.com - Trung Tran
2
正确。为了防止有人不小心发送给所有人而不是想要的人,SendGrid不会使用原生的To:头部。他们希望您使用Personalizations对象,以便您的收件人交叉可见性意图明确。 - jacobmovingfwd

0

Sendgrid API V3

希望这能帮到你。

https://www.npmjs.com/package/sendgrid-v3-node

Example: https://runkit.com/embed/ne9asbfj59fr

var sendgrid = require("sendgrid-v3-node")

const mailOptions = {
    sendgrid_key: 'SENDGRID_KEY',
    from_email: 'FROM_EMAIL',
    from_name: 'FROM_NAME',
    to: ['TO_EMAIL1', 'TO_EMAIL2']
};

mailOptions.subject = 'SUBJECT';
mailOptions.content = 'CONTENT';
sendgrid.send_via_sendgrid(mailOptions).then(response => {
    console.log(response);
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接