Categories
JavaScript Answers

How to pass variable to HTML template in Nodemailer and JavaScript?

Spread the love

Sometimes, we want to pass variable to HTML template in Nodemailer and JavaScript.

In this article, we’ll look at how to pass variable to HTML template in Nodemailer and JavaScript.

How to pass variable to HTML template in Nodemailer and JavaScript?

To pass variable to HTML template in Nodemailer and JavaScript, we put them in the context object property.

For instance, we write

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

to put username and whatever into context.

Then in yourTemplate, we write

{{ username }}

to render the username value.

Conclusion

To pass variable to HTML template in Nodemailer and JavaScript, we put them in the context object property.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published. Required fields are marked *