【问题标题】:How to email multiple recipients in sendgrid v3 node.js如何在 sendgrid v3 node.js 中向多个收件人发送电子邮件
【发布时间】:2016-12-04 06:47:39
【问题描述】:

有人可以帮我在 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 没有收到电子邮件。

有人可以帮忙吗?

提前致谢!

【问题讨论】:

标签: node.js email sendgrid


【解决方案1】:

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/

【讨论】:

    【解决方案2】:
    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);
    };
    

    【讨论】:

      【解决方案3】:

      Sendgrid API V3

      希望这会有所帮助。

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

      示例: 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);
      });
      

      【讨论】:

        【解决方案4】:

        您使用的是 SendGrid 的 Helper Library 吗?你会想要利用Personalizations

        如果您希望收件人互相看到,您应该在单个个性化对象中命名并填充每个收件人。如果您不希望他们看到彼此,并且希望他们每个人都能清楚地收到消息,那么您需要为每个不同的收件人组创建一个新的 Personalization 对象。

        【讨论】:

        • 是的,我正在使用 SendGrid 的帮助程序库 - 所以你是说在单个字符串中列出每个人的电子邮件行不通?如:to: email1@gmail.com; email2@gmail.com; email3@gmail.com?
        • 正确。为了防止有人无意中向所有人发送TO,SendGrid 不会以这种方式构建本机 To: 标头。他们希望您使用 Personalizations 对象,以明确您的收件人交叉可见性的意图。
        【解决方案5】:
        {
          "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
            }
          ]
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-03-01
          • 2018-10-06
          • 2012-05-18
          • 2015-06-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多