Git Product home page Git Product logo

emailer-hbs's Introduction

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

emailer-hbs's People

Contributors

danibram avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.