npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

emailer-hbs

v1.0.13

Published

## What is this?

Downloads

4

Readme

emailer-hbs

What is this?

emailer-hbs is a super hero that helps you to have the full control of your emails in your API. Its powered by:

  • HandlebarsJS
  • Juice
  • Nodemailer
  • Typescript

It takes an array of configuration and a folder with header footer or more partials and that template, it inlines css with juice, and compose the final email.

Configuration for the Mailer

The configuration is very easy:

{
    overrideEmail: string|null,
    from: string|null,
    css: string[]
    input: string
    output: string
    templates: string
    partials: string
    transport: any,
    emailsDirectory: string
}

That is the explanation:

  • overrideEmail: If this string exists, it override the email you pass by the email you want, easy for devs to test it
  • from: general from, can be override if you, send from parameter to renderAndSend
  • css: Array of css paths you want to use in the emails
  • input: Where are your templates
  • output: Where you want to compile that templates
  • templates: Where inside the input are the templates
  • partials: Where inside the input are the partials
  • transport: Nodemailer Transport with your config ready
  • emailsDirectory: Path of your emails configuration

Emails configuration

An example of an email configuration:

{
    title: string
    template: string
    body: string
    data: string[]
    dummyData: {}
    composeData(input: Object): Object
}

That is the explanation:

  • title: Title of your email
  • template: Name of the template, inside your template folder
  • data: Array of the key you need to pass as config
  • dummyData: Dummy data for the emails, to test it
  • composeData: Function to parse the input data

How to use it

npm install emailer-hbs

import { Emailer } from 'emailer-hbs'

import config from '../config'

if (config.env !== 'development' && config.mailer.overrideEmail) {
    config.mailer.overrideEmail = null
}

export default new Emailer(config.mailer)

OR

import Mailer from 'emailer-hbs'
import config from '../config'

if (config.env !== 'development' && config.mailer.overrideEmail) {
    config.mailer.overrideEmail = null
}

let mailer = Mailer(config.mailer)

Folders

This is han example, of the folders i use, in src you have the templates and the common parts, of the emails, then when you cache the emails goes to cache folder, partials are like swig partials but for use in handlebars really easy, end the css with the juice power tranform the css in inline css.

templates
├── cache
│   ├── partials
│   │   ├── base.html
│   │   └── footer.html
│   └── templates
│       ├── access.html
│       └── welcome.html
└── src
    ├── css
    │   ├── ink-emails.css
    │   ├── ink.css
    │   └── style.css
    ├── partials
    │   ├── base.html
    │   └── footer.html
    └── templates
        ├── access.html
        └── welcome.html
// Input
// base.html
<head>
</head>
<body>
    {{ Compile __body__ }}
</body>
</html>

// footer.html
<footer>emailer-hbs 2017</footer>

// welcome.html
<h1>Hi</h1>
{{> footer}}
// Output
// welcome.html
<head>
</head>
<body>
    <h1>Hi</h1>
    <footer>emailer-hbs 2017</footer>
</body>
</html>

Methods

When you initialize the mailer then you can call this methods:

  • renderEmail (internal): render html with handlebars
  • sendEmail (internal): send an email
  • cacheEmails: cache emails, put css inline with juice and generate the final email
  • getconfig: get the config for that template
  • renderTemplate: render template
  • renderAndSend: render the template and send

Changelog

  • 1.0.9: Now support for names with dots
  • 21/11/2017 1.0.8: added support for export default in configurations
  • 08/09/2017: added global from (changes on RenderAndSend)
  • 02/12/2017: initial release