Alongkorn Chetasumon Alongkorn Chetasumon - 5 months ago 73
Node.js Question

node.js sendgrid with multiple recipients get blank value when adding substitution

i found issue when sending email to multiple recipients using sendgrid.
i got blank value when adding substitution.

technologies


  • node.js

  • sendgrid (v2)



==== my sample code (node.js) ====

const SENDGRID_API_KEY = 'KEY'
const sendgrid = require('sendgrid')(SENDGRID_API_KEY)

function sendEmailToSupport() {
const email = new sendgrid.Email({
from: 'jaewwalletsupport@paysbuy.co.th',
to: ['user_a@gmail.com', 'user_b@gmail.com', 'user_c@gmail.com']
html: '<div>test = :test</div>',
subject: 'dummy'
})

email.addSubstitution(':test', 'ddddddddddddd')
sendgrid.send(email, (err, response) => {
if (err) {
console.log(err)
} else {
console.log('Yay! Our templated email has been sent')
}
})
}

module.exports = {
sendEmailToSupport
}


====== result ======


  • user_a@gmail.com gets email with correct content
    test = ddddddddddddd

  • user_b@gmail.com, user_c@gmail.com get email with blank value
    test =



it looks like the first email in the
email.to
array will get the correct content, others get blank data.

in the sendgrid web admin, there is no error, everying is fiine.

how to fix this issue?

thanks

Answer

after analyse the sendgrid lib, i found the solution now by editing this line

email.addSubstitution(':test', new Array(email.to.length).fill('ddddddddddddd'))