Sendgrid-nodejs: Substitution isn't working

Created on 28 Jul 2018  路  7Comments  路  Source: sendgrid/sendgrid-nodejs

I'm currently using NodeJS to make the request and everything seems to be working fine except for the substitutions. When I test it out, I get the template I've created emailed to me, but sendgrid doesn't use my substitutions that I've specified in the code.

Code responsible for sending email:

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);

module.exports =  {
    sendMail(payload) {
        const msg = {
            substitutionWrappers: ["{{", "}}"],
            to: payload.customerEmail,
            from: 'REDACTED',
            templateId: 'REDACTED',
            substitutions: {
              customerName: payload.customerName,
              tourName: payload.tourName,
              departureDate: payload.departureDate,
              pickupLocation: payload.pickupLocation,
              pickupTime: payload.pickupTime
            },
        };

        sgMail.send(msg)
        .then(() => {
            console.log('Mail send successfully');
        })
        .catch((error) => {
            console.log('ERROR: ', error);
        })
    }
}

Sendgrid template data:

Dear {{customerName}},

Congratulations! Your reservation is booked.

Tour: {{tourName}}
Date of Departure: {{departureDate}}
Pick-up Location: {{pickupLocation}}
Pick-up Time: {{pickupTime}}

Thank you for your business.

Picture of the email I recieve:
image

question

Most helpful comment

I actually fixed it for myself. Maybe I was looking at outdated documentation. Basically, the rest API has you use dynamic_template_data instead of substitutions. I tested it and it seems to work. My msg object now looks like:

const msg = {
            to: [email protected],
            from: '[email protected]',
            templateId: 'my-template-id',
            dynamic_template_data: {
                foo: 'bar',
            }
};

All 7 comments

Same issue here. I've data preview seems to work in the designer with the same data but not when sending via the NodeJS module.

I've fixed this issue by using a legacy template instead of a transactional template.

I used the same code as well.

I actually fixed it for myself. Maybe I was looking at outdated documentation. Basically, the rest API has you use dynamic_template_data instead of substitutions. I tested it and it seems to work. My msg object now looks like:

const msg = {
            to: [email protected],
            from: '[email protected]',
            templateId: 'my-template-id',
            dynamic_template_data: {
                foo: 'bar',
            }
};

Alright I'll try this out, thanks for the response!

Thanks for helping out @jackmusick!

@Doxify @jackmusick,

Please follow this issue for progress on properly documenting the dynamic template functionality.

+1 Had the same issue and resolved it by using dynamic_template_data instead of substitutions

Yes!!!!
dynamicTemplateData fixed the issue instead of substitutions

Many thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kiranshashiny picture kiranshashiny  路  4Comments

egges picture egges  路  3Comments

Chrischuck picture Chrischuck  路  3Comments

wooyah picture wooyah  路  4Comments

TobiahRex picture TobiahRex  路  3Comments