Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Substitutions not working #725

Closed
mi-mazouz opened this issue Aug 9, 2018 · 9 comments
Closed

Substitutions not working #725

mi-mazouz opened this issue Aug 9, 2018 · 9 comments
Labels
type: question question directed at the library

Comments

@mi-mazouz
Copy link

mi-mazouz commented Aug 9, 2018

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 #676 (comment)

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!

@canotech
Copy link

canotech commented Aug 9, 2018

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.

@mohsinAnomaly
Copy link

mohsinAnomaly commented Aug 10, 2018

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);

@mi-mazouz
Copy link
Author

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.

@thinkingserious
Copy link
Contributor

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

@thinkingserious
Copy link
Contributor

Thanks for helping out @canotech!

@taykcrane
Copy link

@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.

@mi-mazouz
Copy link
Author

@taykcrane I agree with you!!!

@thinkingserious
Copy link
Contributor

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

@taykcrane
Copy link

Thanks for the update @thinkingserious , appreciate the transparency!

@childish-sambino childish-sambino added the type: question question directed at the library label Feb 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question question directed at the library
Projects
None yet
Development

No branches or pull requests

6 participants