With SendGrid API V3, substitutions not working with HTML

44 Views Asked by At

I've got a very simple example that seems like it should work. I'm following their documentation almost exactly. Here is the simplest code I can think of and it does not substitute the email.

import dotenv from "dotenv";

import sgMail from "@sendgrid/mail";

(async () => {
  dotenv.config();
  sgMail.setApiKey(process.env.SENDGRID_API_KEY);

  const msg = {
    to: '[email protected]',
    from: '[email protected]',
    subject: 'Welcome to Our Newsletter',
    html: '<html><head></head><body><p>Hello {{name}},</p><p>Thank you for signing up for our newsletter!</p></body></html>',
    substitutions: {
      '{{name}}': 'John Doe' // Replace {{name}} with the recipient's name
    },
  };
  // @ts-ignore
  sgMail.send(msg)
    .then(() => {
      console.log('Emails sent successfully');
    })
    .catch((error) => {
      console.error('Error sending emails:', error);
    });


})();

And here is what shows up in my inbox

Hello {{name}},

Thank you for signing up for our newsletter!
0

There are 0 best solutions below