sendgrid-nodejs icon indicating copy to clipboard operation
sendgrid-nodejs copied to clipboard

Substitution isn't working

Open Doxify opened this issue 8 years ago • 7 comments

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

Doxify avatar Jul 28 '18 00:07 Doxify

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.

jackmusick avatar Jul 29 '18 01:07 jackmusick

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

I used the same code as well.

Doxify avatar Jul 29 '18 01:07 Doxify

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',
            }
};

jackmusick avatar Jul 29 '18 01:07 jackmusick

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

Doxify avatar Jul 29 '18 02:07 Doxify

Thanks for helping out @jackmusick!

@Doxify @jackmusick,

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

thinkingserious avatar Jul 31 '18 00:07 thinkingserious

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

sweetyams avatar Aug 06 '18 23:08 sweetyams

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

Many thanks!

LaptopTheOne avatar Aug 21 '18 17:08 LaptopTheOne