sendgrid / sendgrid-nodejs

The Official Twilio SendGrid Led, Community Driven Node.js API Library
https://sendgrid.com
MIT License
3k stars 782 forks source link

Subject not being set if using Dynamic templates #843

Closed nyel-gh closed 5 years ago

nyel-gh commented 5 years ago

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);
10kc-zack commented 5 years ago

Same as #834

nyel-gh commented 5 years ago

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

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

10kc-zack commented 5 years ago

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

thinkingserious commented 5 years ago

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

jlouie commented 5 years ago

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.

thinkingserious commented 5 years ago

Hello @jlouie,

Here is a complete example. I hope that helps!

With Best Regards,

Elmer

nicholasess commented 5 years ago

@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

jlouie commented 5 years ago

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.

thinkingserious commented 5 years ago

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

nyel-gh commented 5 years ago

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

nicholasess commented 5 years ago

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

The place where is the settings, is hidden :/

thinkingserious commented 5 years ago

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

steerio commented 5 years ago

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

marek-survicate commented 4 years ago

@thinkingserious

Why is this issue closed? It is not fixed.

pavelpashkovsky commented 4 years ago

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.

alyak46141414 commented 4 years ago

use in subjects {{subject}}

and send as variable like other variables ,

this way it work .

kelyvin commented 4 years ago

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?

s3tjan commented 4 years ago

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

russell07 commented 4 years ago

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

niklas-b commented 4 years ago

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?

harveyramer commented 4 years ago

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.

strongSoda commented 4 years ago

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.

tcukanoot commented 3 years ago

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

Thanks!

image

childish-sambino commented 3 years ago

@tcukanoot Yes.

andreieuganox commented 3 years ago

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!

psdh commented 3 years ago

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? 🤔

RIGUPTA2 commented 3 years ago

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!

UlyssesInvictus commented 3 years ago

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

sshah98 commented 3 years ago

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()))
andretti1977 commented 3 years ago

When i used triple bracket i was still missing the fact that subject property must be placed under dynamic_template_data and not at higher level as wrongly stated in docs

ChitrangWhiteblink commented 3 years ago

Is there any way to put variable in mail subject ?

cocuba commented 3 years ago

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

this works with {{{subject}}} in template in sengrid web

speix commented 2 years ago

November 2021, still not working according to the documents

ghost commented 2 years ago

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

breakline87 commented 2 years ago

For me when using {{subject}} on the template page in the subject field, then setting:

mail.getPersonalization().get(0).addDynamicTemplateData("subject", "MyCoolSubject");

Worked

ghost commented 2 years ago

Thanks! I have solved the problem

On Fri, Dec 17, 2021 at 8:50 PM Attila Novák @.***> wrote:

For me when using {{subject}} on the template page in the subject field, then setting:

mail.getPersonalization().get(0).addDynamicTemplateData("subject", "MyCoolSubject");

Worked

— Reply to this email directly, view it on GitHub https://github.com/sendgrid/sendgrid-nodejs/issues/843#issuecomment-996697930, or unsubscribe https://github.com/notifications/unsubscribe-auth/AWU3J4LCXUTH4GJGKFOZXVTURMWXVANCNFSM4GCHRZHQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: @.***>

pike-msonda commented 2 years ago

@breakline87 solution solved the problem for me.

{ from: "admin@test.com", template_id: "d-xxx", personalizations: [ { to: [{email: "test@test.com"}], dynamic_template_data: { username: "test", subject: "Hello World", }, }, ], };