Sendgrid-nodejs: Subject not being set if using Dynamic templates

Created on 7 Nov 2018  ·  29Comments  ·  Source: sendgrid/sendgrid-nodejs

I was trying to add subject for my email with dynamic templates but it is not working. Is this a known bug already? Can you suggest any workarounds for this issue?

template code:

<html>
<head>
  <title></title>
</head>
<body>
  <p> Hello <span>{{test_name}}!</span></p>
</body>
</html>

send mail function:

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(settings.sendgrid.api_key);
sgMail.setSubstitutionWrappers('{{', '}}');

const msg = {
    to: '[email protected]',
    from: settings.sendgrid.from,
    subject: 'Test Subject',
    templateId: settings.sendgrid.template_id,
    dynamic_template_data: {
    test_name: 'Testing Name'
    },
};

sgMail.send(msg);
question

Most helpful comment

Hello @jlouie,

Does your subject area look something like this in the UI?

screen shot 2019-01-24 at 1 30 06 pm

With Best Regards,

Elmer

All 29 comments

Same as #834

@10kc-zack I see. It is a known issue then.

By the way, what workaround did you do to fix it?

@nyel-dev Setting the template's subject on SendGrid website works. The problem is that it seems that SendGrid's API does not let you supply a template when you trigger a dynamic template.

Hello @nyel-dev,

I believe in the transactional template you would set the subject to be {{ subject }}, then in your code you set data.subject to be "[TEST] My Email Subject".

Does that work for you?

With Best Regards,

Elmer

Hi @thinkingserious, hoping you may have additional insight.

I am transitioning from a legacy template to the transactional template and I am experiencing this same issue now (sendgrid-nodejs v6.3.1, Node: v8.11.4) even after replacing the transactional template subject field with {{subject}}. I have not explicitly set the substitution wrappers via setSubstitutionWrappers() and have just fallen back on the default handlebars set (although I did try setting those as well while testing).

I've confirmed I'm passing a subject property (no quotes or special characters) as msg.subject in my first test case, msg.dynamic_template_data.subject in my second test case and with both set for my third test case and the resultant emails received still do not have the subject replaced as I would expect. The email subject shows "(no subject)".

Testing the template itself on the sendgrid site in Preview mode and setting the Test Data, does replace the text as expected though.

Any help would be appreciated. Thanks!

* EDIT *
I should also mention that substitutions in the body of the message get replaced as expected, just not in the subject line.

Hello @jlouie,

Here is a complete example. I hope that helps!

With Best Regards,

Elmer

@thinkingserious the subject not show in email same with example

to: "[email protected]",
  from: "[email protected]",
  subject: "Hello world",
  text: "Hello plain world!",
  html: "<p>Hello HTML world!</p>",
  templateId: "d-xxxx",
  dynamic_template_data: {
    subject: "Testing Templates & Stuff",
    name: 'Some "Testing" One',
    city: "<b>Denver<b>"
  }

image

Hello @jlouie,

Here is a complete example. I hope that helps!

With Best Regards,

Elmer

@thinkingserious: Unfortunately, it doesn't. I'm still experiencing a missing Subject line in the email sent/received, just like @nicholasess.

Hello @jlouie,

Does your subject area look something like this in the UI?

screen shot 2019-01-24 at 1 30 06 pm

With Best Regards,

Elmer

By the way I was able to solve this problem by using the Set Subject on the Sendgrid template interface. Thanks @thinkingserious

I suggest that the settings change to the menu like code, test data.
image

The place where is the settings, is hidden :/

Thanks for the feedback @nicholasess! I will pass it on to the design team.

I ran into the same problem, and I had to rely on this workaround, but it would not have worked without sifting through issues.

This is super counterintuitive, as the doc says that you set the subject in the API call (and details how you can set it globally for the message and override it in personalizations, which is obviously lost with this workaround).

We also lose the "default subject set on the admin page, which can be overridden in the API call" feature. Nope, with the workaround we always have to specify a subject for the affected templates.

(Unless there's a way to set default in {{ }} interpolation. I'll check the doc for that, what I've found so far is about an interpolation method using dashes, which I'm completely unfamiliar with.)

@thinkingserious

Why is this issue closed? It is not fixed.

I know it might looks weird, but guys make sure you don't have opened windows or tabs with the same screen. Sendgrid ui has autosave which can rewrite all data which you saved before.

use in subjects {{subject}}

and send as variable like other variables ,

this way it work .

I was stuck on this issue for so long because I was following the docs and thought I did something wrong. Since I'm migrating from legacy templates, I thought the API would stay the same and I was surprised to see it suddenly not work as expected.

Are there any plans to update the documentation to v3/mail/send to properly reflect this change?

The only way I was able to solve this issue was with _triple-stash_:

{{{ subject }}}

According to this article https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/transactional-templates.md

Can confirm using triple instead of double curly braces in the subject field works

Whenever I save the triple curlys or any other code in the subject line, the code reverts back automatically after a few minutes to the standard <%subject%>.

Is anyone else having the same issue?

Is anyone else having the same issue?

I have seen something like this when I had two windows open. SendGrid auto-saves, so if you have two windows open, they will cancel each other out.

The way it worked for me:

use in subject field in SendGrid UI: {{{subject}}}

send "subject": "test" in message.dynamic_template_data

Suggestion: You guys should really work on your docs. I mean really they are so scattered and bits and pieces are outdated.

Triple brackets is still working, I'm just checking to see if that is still the official solution.

Thanks!

image

@tcukanoot Yes.

Please do re-open issue! The documents still are not correct suggesting subject to be set on the top level of the request. However, it must be included in dynamic_template_data and subject in template must be set as {{subject}} or any other dynamic key. This is open from 2018 and no any changes in documentation introduced since!

I get a 400 request error from the server when I'm trying to use substitutions or dynamic_template_data

Please fix your docs. dynamic_template_date shouldn't be valid based on the https://github.com/sendgrid/sendgrid-python/blob/main/use_cases/kitchen_sink.md and the v3 documentation
Yet people are reporting that it works for them

what's going on? 🤔

The way it worked for me:

use in subject field in SendGrid UI: {{{subject}}}

send "subject": "test" in message.dynamic_template_data

Suggestion: You guys should really work on your docs. I mean really they are so scattered and bits and pieces are outdated.

How to do it in PHP. Please share the code!

Are the docs currently just incorrect in suggesting that subject be sent as a top level parameter then?

The way it worked for me:

use in subject field in SendGrid UI: {{{subject}}}

send "subject": "test" in message.dynamic_template_data

Suggestion: You guys should really work on your docs. I mean really they are so scattered and bits and pieces are outdated.

This worked for me! Your doc should look something like:

const msg = {
    to: toEmail,
    from: "email",
    templateId: "tempateId",
    dynamic_template_data: {
      subject: "subjectHere",
    },
  };

sendGridEmail
    .send(msg)
    .then(() => console.log("email sent"))
    .catch((error) => console.error(error.toString()))
Was this page helpful?
1 / 5 - 1 ratings

Related issues

mikemaccana picture mikemaccana  ·  4Comments

polkhovsky picture polkhovsky  ·  3Comments

amlcodes picture amlcodes  ·  4Comments

egges picture egges  ·  3Comments

wooyah picture wooyah  ·  4Comments