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没有收到电子邮件。
有人可以帮忙吗?
提前致谢!
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)
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)
在幕后Personalizations,您可以使用帮助程序进行更好的控制:
https://sendgrid.com/docs/for-developers/sending-email/personalizations/