Sendgrid-nodejs: sendMultiple not working properly with transactional Templates

Created on 28 Jun 2019  路  8Comments  路  Source: sendgrid/sendgrid-nodejs

Issue Summary

I'm trying to send a transactional template to multiple users without revealing all users in the "to" field when the email is received. In the docs it says to use the method sgMail.sendMultiple(). I have tested but still showing all emails addresses to all recipients.

here is my code

  const msg = {
      from: {
         email: [email protected],
         name: "My Org"
     },
      reply_to: {
        email: [email protected],
        name: "Admin"
      },
      bcc: ['[email protected]'],
      categories: ['Transactional', 'SpecialOne'],
      template_id: 'd-xxxxxxxxxxxxxxxx',
      personalizations: [{
        to: [
            { email: [email protected], name: username1 },
            { email: [email protected], name: username2 },
            { email: [email protected], name: username3 },
         ],
        dynamic_template_data: data
      }]
    };
sgMail.sendMultiple(msg);

As the documentation said for sendMultiple only the user's email address should be showing when the email is received, however this is not the case.

Thank you

Technical details:

  • sendgrid-nodejs "@sendgrid/mail": "6.4.0",
  • Node.js Version: 10.15.1
  • express": "4.16.4",
question

All 8 comments

I would use BCC instead of To.

Thanks for the reply but i do not think the BCC would not work for me for the reasons below,
1) the template has some customization where {{name}} and some other variables are replaced for each receiver. Question will the BCC replace the variables in the email content?
2) from the documentation it says that "if you wish to distribute BCCs to multiple addresses you will need to create a distribution group or use forwarding rules." and futhermore i would have to turn the feature on and as per the documentation "With this setting turned on, you will be charged an extra email for every email you send.".

My issue is that this is a transactional email meant to be an invitation email coming from one user to other friends. no way of defining a distriution list ahead.

So my best bet is to use the sendMultiple() and not showing all email addresses in the "to" field.

So is this a bug for the sendMultiple not able to work with the templates? or am i missing something?

Thank you for the help

I'm also having issues with sendMultiple() not working as expected. It shows all emails.

Same here but solved it by sending multiple emails to multiple recipients like:

let messages = new Array()

recipients.forEach(recipient => {
  messages.push({
    from: {
      email: '[email protected]',
      name: 'Name'
    },
    reply_to: {
      email: '[email protected]',
      name: 'Name'
    },
    bcc: ['[email protected]'], // optional
    subject: `Some subject with a ${param}`,
    templateId: 'xxxxxxxxx', // not sure if template_id with _ will work
    personalizations: [{
      to: {
        email: recipient.email,
        name: recipient.name
      },
      dynamic_template_data: data
    }]
  })
})

The solution provided by @sergeyfilimonov should work. Another way to do this with a single API call (using the OP's sample code):

const msg = {
    from: {
        email: '[email protected]',
        name: 'My Org'
    },
    reply_to: {
        email: '[email protected]',
        name: 'Admin'
    },
    bcc: ['[email protected]'],
    categories: ['Transactional', 'SpecialOne'],
    template_id: 'd-xxxxxxxxxxxxxxxx',
    personalizations: [
        { to: { email: '[email protected]', name: 'username1' } },
        { to: { email: '[email protected]', name: 'username3' } },
        { to: { email: '[email protected]', name: 'username2' } }
    ],
    dynamic_template_data: data
};
sgMail.send(msg);

I'm not sure why you closed this and tagged it as a question, seems to me that this is a workaround not a solution. If this is a bug and it is not working as documented then remove it from your docs or update it to reflect how it should work. This is not a good way to get adoption of your service.

I don't consider this to be a workaround as it's the proper way to accomplish what you're looking to achieve. The sendMultiple implementation simply wasn't built to work with personalizations and is more of a convenience function when the top-level to value is an array. That said, I'm open to reviewing a PR to enhance the documentation/samples with how to achieve what I illustrated above.

ok thank you for the clarification and the follow-up.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

polkhovsky picture polkhovsky  路  3Comments

Loriot-n picture Loriot-n  路  4Comments

thinkingserious picture thinkingserious  路  4Comments

prasoonjalan picture prasoonjalan  路  3Comments

egges picture egges  路  3Comments