Skip to content Skip to sidebar Skip to footer

Variable Substitution In Sendgrid Templates With Nodejs Does Not Work

Following the USE CASE on SendGrids github does manage to send me the e-mail with the correct template, but the substitutions does apparently not work, and is left blank in the res

Solution 1:

Since what I was using was dynamic templates from SendGrid, I cannot use the "substitutions" tag, but must instead use the "dynamic_template_data" tag, see this issue. When changing the msg-object to

const msg = {
    to: '...',
    from: 'sender@example.org',
    subject: 'Hello world',
    text: 'Hello plain world!',
    html: '<p>Hello HTML world!</p>',
    templateId: '...',
    dynamic_template_data: {
        name: 'Some One',
        city: 'Denver',
    },
};

it works. This is not documented in the SendGrid-documentation as far as I can see.

Solution 2:

Also you can do this:

import { getConfig } from'../config';    
const msg = {
                to: recipient,
                from: global.gConfig['SENDGRID_EMAIL_FROM'], // or getConfig().SENDGRID_EMAIL_FROM
                templateId: this.templateId,
                dynamicTemplateData: this.variables,
    };

getConfig.ts

export function getConfig(): any {return process.env;
}

Post a Comment for "Variable Substitution In Sendgrid Templates With Nodejs Does Not Work"