Categories
JavaScript Answers

How to pass variable to html template in Nodemailer?

Spread the love

To pass variable to html template in Nodemailer, we put them in the context property.

For instance, we write

const mailOptions = {
  from: "abc@gmail.com",
  to: "username@gmail.com",
  subject: "Sending email",
  template: "yourTemplate",
  context: {
    username,
    whatever: variable,
  },
};

const info = await transporter.sendMail(mailOptions);

to call sendMail with mailOptions to send the email message.

We set the context property to an object with the template variables to interpolate them in the template.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.