Sendgrid-nodejs: Substitutions not working

Created on 9 Aug 2018  路  9Comments  路  Source: sendgrid/sendgrid-nodejs

Hello,

Issue Summary

I tried to send an email with some substitutions inside. Like mentionned in the doccumentation:

sgMail.setApiKey(config.SENDGRID_API_KEY)
  sgMail.setSubstitutionWrappers('-!!', '!!-')

  const msg = {
    to: userData.email,
    from: '[email protected]',
    subject: 'Confirm your email',
    templateId: config.EMAIL_TEMPLATES['FR'].CONFIRM_EMAIL_ID,
    substitutions: {
      firstName: userData.firstName,
      link: `${config.APP_END_POINT}/confirm-email?token=${token}`
    }
  }

  sgMail
  .send(msg)
  .then(() => logger.info('Confirmation email sent successfully'))
  .catch((error) => logger.error(error.message))

This way does not work, I still see in my email -!!firstName!!-
Just after I saw that I tried to change the values of setSubstitutionWrappers's params: Like that:

sgMail.setApiKey(config.SENDGRID_API_KEY)
  sgMail.setSubstitutionWrappers('{{', '}}')

  const msg = {
    to: userData.email,
    from: '[email protected]',
    subject: 'Confirm your email',
    templateId: config.EMAIL_TEMPLATES['FR'].CONFIRM_EMAIL_ID,
    substitutions: {
      firstName: userData.firstName,
      link: `${config.APP_END_POINT}/confirm-email?token=${token}`
    }
  }

  sgMail
  .send(msg)
  .then(() => logger.info('Confirmation email sent successfully'))
  .catch((error) => logger.error(error.message))

This time, I see like an empty string instead !!-firstName-!!
So I tried an other way which is mentionned in this issue https://github.com/sendgrid/sendgrid-nodejs/issues/676#issuecomment-407704072

sgMail.setApiKey(config.SENDGRID_API_KEY)
  sgMail.setSubstitutionWrappers('-!!', '!!-')

  const msg = {
    to: userData.email,
    from: '[email protected]',
    subject: 'Confirm your email',
    templateId: config.EMAIL_TEMPLATES['FR'].CONFIRM_EMAIL_ID,
    dynamicTemplateData: {
      firstName: userData.firstName,
      link: `${config.APP_END_POINT}/confirm-email?token=${token}`
    }
  }

  sgMail
  .send(msg)
  .then(() => logger.info('Confirmation email sent successfully'))
  .catch((error) => logger.error(error.message))

This way does not work as well but the fourth attempt was a sucess!!!
('{{', '}}' && dynamicTemplateData together)

sgMail.setApiKey(config.SENDGRID_API_KEY)
  sgMail.setSubstitutionWrappers('{{', '}}')

  const msg = {
    to: userData.email,
    from: '[email protected]',
    subject: 'Confirm your email',
    templateId: config.EMAIL_TEMPLATES['FR'].CONFIRM_EMAIL_ID,
    dynamicTemplateData: {
      firstName: userData.firstName,
      link: `${config.APP_END_POINT}/confirm-email?token=${token}`
    }
  }

  sgMail
  .send(msg)
  .then(() => logger.info('Confirmation email sent successfully'))
  .catch((error) => logger.error(error.message))

Maybe I did something wrong or there is something that I didn't understood. For me substitutionsWappers can be whatever we want and the substitutions field should work.

Thanks!

question

Most helpful comment

at template side use {{Var_Name}} and in node side use

const msg = {
from: sendgridUtils.**,
templateId: sendgridUtils.TPL_ID,
subject: '',
html: '',
personalizations: [
{ to: [{email: owner.email}],
dynamic_template_data: {
'Var_Name': value
}
}
]
};
sgMail.send(msg, cb);

All 9 comments

You need to use dynamic_template_data instead of substitutions, this isn't yet documented anywhere!

Then use {{var_name}} in email template and dynamic_template_data: { var_name: "var_value" } in API call.

at template side use {{Var_Name}} and in node side use

const msg = {
from: sendgridUtils.**,
templateId: sendgridUtils.TPL_ID,
subject: '',
html: '',
personalizations: [
{ to: [{email: owner.email}],
dynamic_template_data: {
'Var_Name': value
}
}
]
};
sgMail.send(msg, cb);

Thank you for answering. I found the solution, my question was rather why the documentation is not up to date and why the substitution wrapper can not be of any value.

Hello @mi-mazouz,

It's not updated because the issue is still on our backlog.

We thank you for your feedback and comments as they help increase the priority of that issue.

With Best Regards,

Elmer

Thanks for helping out @canotech!

@thinkingserious Just want you to know I spent an hour trying to debug why substitutions wasn't working, and then an hour trying to make dynamic_template_data work with such lackluster documentation. Expected way more from Sendgrid. If you're going to release a new feature without documentation, don't deprecate the other feature it replaces before the new documentation comes out.

@taykcrane I agree with you!!!

Hello @taykcrane,

Thank you for taking the time to express your feedback, it is greatly appreciated.

My apologies for the poor experience. I chose to not try and block the release of this feature due to the SDKs not being completely ready. It is my failure that the SDKs do not have updated documentation and helper functions currently.

With any luck, it should not be longer than a few weeks to get this SDK up to speed fully.

As for the previous types of templates, they are still available but are now called legacy templates.

With Best Regards,

Elmer

Thanks for the update @thinkingserious , appreciate the transparency!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

egges picture egges  路  3Comments

kiranshashiny picture kiranshashiny  路  4Comments

agostonbonomi picture agostonbonomi  路  3Comments

thinkingserious picture thinkingserious  路  4Comments

mikemaccana picture mikemaccana  路  4Comments