sendgrid-nodejs: Dynamic Template Data is not showing!

template html code: <div style="text-align: center;"><span style="font-size:28px;"><strong><p>Code: {{voucherCode}}</p></strong></span></div>

Nodejs Code:

    sgMail.setApiKey(process.env.SENDGRID_API_KEY);
    //sgMail.setSubstitutionWrappers('{{', '}}'); // Configure the substitution tag wrappers globally
    const msg = {
        to: 'exampel@exmaple.com',
        from: 'sender@example.org',
        templateId: 'd-787c96877a394be5b95c91750622fed1',
        dynamicTemplateData : {
            voucherCode: "Some One"
        },
    };
    sgMail.send(msg)

When I received the email, the voucherCode is empty!

About this issue

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

Most upvoted comments

It looks like the dynamic_template_data must be inside a personalizations array in order for it to work.

This works for me:

from: 'noreply@email.com',
subject: 'Thanks for your recent review',
templateId: 'template_id',
personalizations: [
            {
              to: {
                name: 'Some One',
                email: 'email@email.com'
              },
              dynamic_template_data: {
                review: {
                  url: 'https://www.google.com'
                }
              }
            }
          ]

Above poster is correct. There is a bug either in the library code or typescript definitions.

export interface PersonalizationData {
  to: EmailData | EmailData[],
  cc?: EmailData | EmailData[],
  bcc?: EmailData | EmailData[],
  subject?: string;
  headers?: { [key: string]: string };
  substitutions?: { [key: string]: string };
  dynamicTemplateData?: { [key: string]: string; };
  customArgs?: { [key: string]: string };
  sendAt?: number;
}

This would suggest that you can pass dynamicTemplateData as a value in PersonalizationData, but it has to actually be snake case, dynamic_template_data.

The solution that worked for me is placing the dynamic_template_data inside the personalization object in the exact way seen below:

    "personalizations": [
        {
              "dynamic_template_data": {
            "fullname": "full Name",
            "useremail": ":info@test.coml",
            "userphone": "56456",
            "usermsg": "tex fdsfgasdf t"
    },
            "to": [
                {
                    "email": "test1@test.com"
                }
            ],
            "cc": [
                {
                    "email": "test@test.com"
                }
            ]
        }
]

Yup, I’m seeing the same issue. The To and Bcc get the email, but the dynamic template is not populated in the Bcc when using the global Bcc account setting. Unfortunately, that’s not something that we can resolve in this helper lib so best to create a support ticket to request that this bug be fixed in the backend.

I spent 2 hours trying to debug this issue only to realize that the issue occurs on a BCC’ed email.