Socket
Socket
Sign inDemoInstall

handlebars-email

Package Overview
Dependencies
6
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    handlebars-email

A Handlebars template engine for emails.


Version published
Weekly downloads
8
increased by33.33%
Maintainers
1
Install size
5.03 MB
Created
Weekly downloads
 

Readme

Source

Handlebars Email

A Handlebars template engine for emails.

Handlebars provides the power necessary to let you build semantic templates effectively with no frustration.
Checkout the official Handlebars docs site at handlebarsjs.com or Give it a Try.

Installation

Use the npm package manager to install Handlebars Email.

npm i handlebars-email

Usage

Import Handlebars Email hbsEmail method.
hbsEmail( template, context ) take 2 argument a template & a context argument. Which will be used to render the final template.

  • template - Path of the template with extension ( .hbs or .handlebars ) or template name if hbsEmailConfig is set.
  • context - The actual context Object.

hbsEmailConfig()

  • views - Views Path of the email template folder
  • extname - template extension ( .hbs or .handlebars )
hbsEmailConfig({
  views: "views/email/",
  extname: ".hbs"
});

Once you have a template, use the hbsEmail method to render the template by passing the template & context.

Example

email.js

const { hbsEmail } = require("handlebars-email");
const path = require("path");

const template = path.join("views", "email", "template.hbs");
const context = { message: "Hello World!" };
const eMailTemplate = hbsEmail(template, context);

With hbsEmailConfig

const { hbsEmail, hbsEmailConfig } = require("handlebars-email");

hbsEmailConfig({
  views: "views/email/",
  extname: ".hbs",
});

const context = { message: "Hello World!" };
const eMailTemplate = hbsEmail("template", context);

template.hbs

<html>
  <head>
    <title>Message HTML Title</title>
  </head>
  <body>
    <div>
      <h2>Message: </h2>
      <p>{{message}}</p>
    </div>
  </body>
</html>

Would render:

<html>
  <head>
    <title>Message HTML Title</title>
  </head>
  <body>
    <div>
      <h2>Message:</h2>
      <p>Hello World!</p>
    </div>
  </body>
</html>

With Nodemailer

email.js

const { hbsEmail } = require("handlebars-email");
const nodemailer = require("nodemailer");
const path = require("path");

const template = path.join(__dirname, "/template.hbs");
const context = { message: "Hello World!" };
const eMailTemplate = hbsEmail(template, context);

const transporter = nodemailer.createTransport({
  host: process.env.SMTP_HOST,
  port: process.env.SMTP_PORT || 587,
  secure: process.env.SMTP_PORT === 465, // true for 465, false for other ports
  auth: {
    user: process.env.SMTP_USERNAME,
    pass: process.env.SMTP_PASSWORD,
  },
});

const mailOptions = {
  from: "sender@example.com", // Sender address
  to: "receiver@example.com", // List of recipients
  subject: "Node Mailer Handlebars Email", // Subject line
  html: eMailTemplate, // Handlebars eMail template
};

transporter.sendMail(mailOptions, (error, email) => {
  if (error) return console.log(error);
  console.log("Message sent: %s", email.messageId);
});

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch ( git checkout -b feature/AmazingFeature)
  3. Commit your Changes ( git commit -m 'Add some AmazingFeature')
  4. Push to the Branch ( git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE.md for more information.

Keywords

FAQs

Last updated on 30 Nov 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc