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

时间:2016-12-04 06:47:39

标签: node.js email 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);
        }
    });

}

在上面的示例中,只有emailUser1@gmail.com收到电子邮件; emailUser2@gmail.comemailUser3@gmail.com未收到该电子邮件。

有人可以帮忙吗?

提前致谢!

5 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

class CustomUser(AbstractUser):
    username = None
    email = models.EmailField(_('email address'), unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = CustomUserManager()

    def __str__(self):
        return self.email

答案 2 :(得分:0)

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/

答案 3 :(得分:0)

{
  "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
    }
  ]
}

答案 4 :(得分:-1)