sendgrid-nodejs icon indicating copy to clipboard operation
sendgrid-nodejs copied to clipboard

Add Dynamic Template Support

Open thinkingserious opened this issue 8 years ago • 30 comments

Issue Summary

On 7/24/2018, our team publicly launched dynamic content for transactional templates. It is now available for all customers sending over v3 of our Mail Send API. Iterate over lists, handle conditionals and more, thanks to native support for a subset of Handlebars syntax!

More information can be found in our blog post announcement.

You can currently use this feature by manually creating the request body as shown here.

Now, we need to create helper code (this is complete) and examples for this SDK.

Acceptance Criteria

Documentation

thinkingserious avatar Jul 24 '18 21:07 thinkingserious

Please update your documentation- I just spent an hour trying to figure out why substitutions were not working with the v3 API.

jharris-code avatar Aug 02 '18 18:08 jharris-code

My apologies for the poor experience @jharris-code.

I've added your vote to this issue to help it gain priority. I think it will be updated soon because we sone have PR #711.

thinkingserious avatar Aug 04 '18 18:08 thinkingserious

It is very silly that the absense of documentation blocks the release of the actual code.

catamphetamine avatar Aug 07 '18 09:08 catamphetamine

Hi @catamphetamine,

The code was released in v6.3.1. I hope that helps, thanks!

With Best Regards,

Elmer

thinkingserious avatar Aug 07 '18 13:08 thinkingserious

@thinkingserious Oh, cool, actually it didn't cross my mind to update the version of the library. I'll try that out, thx.

catamphetamine avatar Aug 07 '18 13:08 catamphetamine

Note to users: when using templates pass dynamic_template_data instead of substitutions.

catamphetamine avatar Aug 07 '18 14:08 catamphetamine

Now you have to change "substitutions:" to "dynamic_template_data:"

And the templates uses handlebars no more need to specify "substitutionWrappers"

Beethoven avatar Aug 25 '18 12:08 Beethoven

This example in your use cases still uses substitutions rather than dynamic_template_data. Plz update, it definitely took me a couple hours of playing with the SDK and searching before I found this thread. (Also your api docs don't mention anything about this, which didn't help either. Also, substitutionWrappers doesn't seem to work with dynamic_template_data at all. Regardless of including the key value pair substitutionWrappers: ['*|', '|*'] in my message object, only template vars wrapped in curly braces were filled in. (Are you guys now intentionally forcing everybody to use handlebar syntax for templates?)

josh-yonomi avatar Aug 29 '18 23:08 josh-yonomi

My apologies @josh-yonomi,

I've updated the documentation based on your feedback.

For our new templates, they use handlebars syntax. The legacy templates still work as before.

With Best Regards,

Elmer

thinkingserious avatar Aug 30 '18 00:08 thinkingserious

I receive the email but the substitutions does not work. What can cause the problem?

const msg = {
    to: email,
    from: sendGridMail,
    templateId: emailTemplate.confirmationEmail,
    dynamic_template_data: {
      firstName: firstName,
      lastName: lastName,
      link: link
    }
  };

ldragos1 avatar Aug 30 '18 12:08 ldragos1

EDIT via @thinkingserious

Do you mind letting us know where the problem is? We'd love to understand your frustrations so that we can improve.

larafale avatar Aug 31 '18 15:08 larafale

@drav96,

Do you mind sharing what your template looks like?

With Best Regards,

Elmer

thinkingserious avatar Aug 31 '18 15:08 thinkingserious

why do you strip special chars in substitutionWrappers ??

dynamic_template_data: {
      'foo-bar': 'wtf',
      'bar_baz': 'wtf',
      'baz.bro': 'wtf',
      'foo': 'wtf'
    }

only {{foo}} return wtf string in emails.

larafale avatar Aug 31 '18 16:08 larafale

I'm not sure @larafale, but that certainly does not seem reasonable. Looking at the source code for this SDK, I don't see where those keys are getting modified.

Do you mind creating a separate issue for this issue and include what your HTML template looks like? I'll mark the new issue as a bug and try to reproduce and fix if necessary.

thinkingserious avatar Aug 31 '18 16:08 thinkingserious

Hello,

So, I am trying to send a dynamic template email, however, I am not been able to use substitutions or dynamic_template_data. My template has tags such as {{fullname}} or {{date}} and these properties are been sent to the send function:

const msg = {
		to,
		from,
		templateId: template.id,
		dynamic_template_data: substitutions,
	};

	return sgMail.send(msg)

I confirmed that the substitutions object has the right properties with the right values but the substitution doesn't work.

Any idea what I am doing wrong?

Thanks.

gianfelipe93 avatar Sep 10 '18 12:09 gianfelipe93

@gianfelipe93 The structure is correct. I had the same problem. My solution was to uninstall the @sendgrid package from my project and install it again Let me know if it works for you

const msg= {
       to: email,
       from: sendGridMail,
       templateId: emailTemplate.requestDemoEmail,
       dynamic_template_data: {
         name: data.name,
         email: data.email,
       }
     };

ldragos1 avatar Sep 10 '18 12:09 ldragos1

@drav96 thanks mate, it's working now

gianfelipe93 avatar Sep 10 '18 12:09 gianfelipe93

Adding what I hope will be some definitive documentation after wasting another hour of my time on this. (First, thanks to everyone who wasted hours of their time before me.)

  1. If your template id starts with d-, then substitutions won't work, and you should use CAMEL-CASE dynamicTemplateData (see here, where they convert snake_case keys to camelCase anyway)
  2. If your template starts with d-, then setSubstitutionWrappers is silently ignored, and you must use {{ and }} in your templates

kael-shipman avatar Sep 12 '18 01:09 kael-shipman

Adding what I hope will be some definitive documentation after wasting another hour of my time on this. (First, thanks to everyone who wasted hours of their time before me.)

  1. If your template id starts with d-, then substitutions won't work, and you should use CAMEL-CASE dynamicTemplateData (see here, where they convert snake_case keys to camelCase anyway)
  2. If your template starts with d-, then setSubstitutionWrappers is silently ignored, and you must use {{ and }} in your templates

In my case it works with dynamic_template_data even if has in template id the letter d-

ldragos1 avatar Sep 12 '18 10:09 ldragos1

Yes, it does work with snake case keys, but it appears as though the devs have committed internally to camel case (see the line I linked to in my report above). Thus, I would recommend all new code use camel case.

kael-shipman avatar Sep 12 '18 14:09 kael-shipman