问题
Can someone help me send an email to multiple recipients in sendgrid v3 + node.js? I've noticed that when I enter several email addresses in the to
field, only the first email address receives the email. The email addresses after the first one do not receive the email:
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);
}
});
}
In the example above, only emailUser1@gmail.com
receives the email; emailUser2@gmail.com
and emailUser3@gmail.com
do not receive the email.
Can someone help?
Thanks in advance!
回答1:
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:
Are you using SendGrid's Helper Library? You're going to want to leverage Personalizations.
If you want the recipients to see each other, you should name & populate each of the recipients within a single Personalization object. If you don't want them to see each other, and want them to each receive the message distinctly, you'll want to make a new Personalization object for each distinct recipient group.
回答3:
Sendgrid API V3
Hope this helps.
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);
});
来源:https://stackoverflow.com/questions/40956191/how-to-email-multiple-recipients-in-sendgrid-v3-node-js