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

Subject not being set if using Dynamic templates

Open nyel-gh opened this issue 8 years ago • 37 comments

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

nyel-gh avatar Nov 07 '18 07:11 nyel-gh

Same as #834

10kc-zack avatar Nov 07 '18 21:11 10kc-zack

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

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

nyel-gh avatar Nov 08 '18 15:11 nyel-gh

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

10kc-zack avatar Nov 08 '18 19:11 10kc-zack

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

thinkingserious avatar Nov 13 '18 23:11 thinkingserious

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.

jlouie avatar Jan 08 '19 02:01 jlouie

Hello @jlouie,

Here is a complete example. I hope that helps!

With Best Regards,

Elmer

thinkingserious avatar Jan 17 '19 22:01 thinkingserious

@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

nicholasess avatar Jan 24 '19 00:01 nicholasess

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.

jlouie avatar Jan 24 '19 00:01 jlouie

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

thinkingserious avatar Jan 24 '19 21:01 thinkingserious

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

nyel-gh avatar Jan 26 '19 15:01 nyel-gh

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

The place where is the settings, is hidden :/

nicholasess avatar Jan 26 '19 19:01 nicholasess

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

thinkingserious avatar Jan 28 '19 19:01 thinkingserious

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

steerio avatar Sep 11 '19 14:09 steerio

@thinkingserious

Why is this issue closed? It is not fixed.

marek-survicate avatar Jan 15 '20 13:01 marek-survicate

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.

pavelpashkovsky avatar Jan 15 '20 13:01 pavelpashkovsky

use in subjects {{subject}}

and send as variable like other variables ,

this way it work .

alyak46141414 avatar Jan 15 '20 13:01 alyak46141414

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?

kelvicthrust avatar Feb 21 '20 00:02 kelvicthrust

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

s3tjan avatar Mar 19 '20 17:03 s3tjan

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

russell07 avatar May 13 '20 07:05 russell07

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?

niklas-b avatar Jun 03 '20 17:06 niklas-b