sendgrid-nodejs: Problem sending bulk email along with substitutions
Issue Summary
While trying to send bulk emails, I want to personalize individual email by their first name etc by using substitutions. I’m setting firstName array under ‘sub’ and sending the request to mail API but it’s throwing the error- SendGridError: Response error
Here is the stack trace of the error-
“error”:“SendGridError: Response error\n at /node_modules/sendgrid/lib/sendgrid.js:104:23\n at IncomingMessage.<anonymous> (/node_modules/sendgrid-rest/lib/client.js:108:9)\n at emitNone (events.js:91:20)\n at IncomingMessage.emit (events.js:188:7)\n at endReadableNT (_stream_readable.js:975:12)\n at _combinedTickCallback (internal/process/next_tick.js:80:11)\n at process._tickDomainCallback (internal/process/next_tick.js:128:9)”
Here is the function that I’m using-
const from = "mail@example.com";
const to = ["email1@example.com", "email2@example.com"];
const TEMPLATE_ID = "some_template_id";
const substititions = {"-firstName-": ["name1", "name2"]};
const subject = "Testing bulk email";
sendMail(from, to, TEMPLATE_ID, substitutions, subject);
function sendMail(from ,to, TEMPLATE_ID, substitutions, subject) {
var request = sendgrid.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: {
'personalizations': [
{
'to': to,
'sub': substitutions,
'subject': subject
}
],
'from': {
'email': from
},
'template_id': TEMPLATE_ID
}
});
return sendgrid.API(request);
}
Refrences:-
These are the two links that I’ve followed- sendgrid API Reference sendgrid nodejs lib without mail helper class
Technical details:
- sendgrid Version: 5.0.1
- Node.js Version: 6.9.5
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 16 (8 by maintainers)
Finally got it working, below is my solution-