5

I'm trying to insert a dynamical variable into the subject line via this library.

For example: "Hello {{name}}, welcome to {{store_name}}"

My email sends off no problem, but the subject renders {{name}} and {{store_name}} as... just that. No variables are inserted.

Code example:

             dynamic_template_data: {
                name: "John",
                store_name: "My store",
                subject: "Hello {{name}}, welcome to {{store_name}}!"
              }

In my template within Sendgrid UI, the subject value is: {{subject}} I've also tried: {{{subject}}} but no go.

Email is sent and the subject line is... Hello {{name}}, welcome to {{store_name}}

When it should be... Hello John, welcome to My store!

Pat
  • 225
  • 3
  • 11

2 Answers2

0

The reason I think is that you're trying to do a double resolve of a variable. I believe it should be either

  1. You have {{subject}} as a subject of your template and then you dynamically generate the subject in the code and pass it
              dynamic_template_data: {
                name: "John",
                store_name: "My store",
                subject: "Hello John, welcome to My store!"
              }

OR

  1. You have Hello {{name}}, welcome to {{store_name}}! as a subject of a template and then it should be substituted (no need to pass the subject separately)
              dynamic_template_data: {
                name: "John",
                store_name: "My store",
              }
Andrey Stukalin
  • 5,328
  • 2
  • 31
  • 50
0

You must set in your Sendgrid dynamic template the subject as: {{{subject}}}

Like this:

enter image description here