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

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: 'test@email.com',
    from: settings.sendgrid.from,
    subject: 'Test Subject',
    templateId: settings.sendgrid.template_id,
    dynamic_template_data: {
	test_name: 'Testing Name'
    },
};

sgMail.send(msg);

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 19
  • Comments: 37 (5 by maintainers)

Most upvoted comments

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

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.

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

@thinkingserious

Why is this issue closed? It is not fixed.

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

November 2021, still not working according to the documents

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

The place where is the settings, is hidden 😕

Hello @jlouie,

Here is a complete example. I hope that helps!

With Best Regards,

Elmer

use in subjects {{subject}}

and send as variable like other variables ,

this way it work .

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

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!

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

Thanks!

image

I notice the subject always use the dynamic template setting from official message = Mail( from_email=FROM_EMAIL, to_emails=TO_EMAILS, subject='a test subject' ) The param “subject” in above code is not working

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

@thinkingserious the subject not show in email same with example

to: "email@example.com",
  from: "email@example.com",
  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