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

jamoose

v2.2.0

Published

Preprocessing and Sending HTML Emails in Node.js

Downloads

10

Readme

jamoose Build Status Built with Grunt

Preprocessing and Sending HTML Emails in Node.js

Goals

  1. Send HTML Emails
  2. Use preprocessors like Jade and Sass to write them
  3. Only care about the template and its data at time of sending

Getting Started

This plugin requires Grunt ~0.4.2

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install jamoose --save

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

require('jamoose')(grunt);

If you can figure out how I can use loadNpmTasks, please open a pull request

You can require it in your application like so:

var jamoose = require('jamoose'),
    Mailer = new jamoose({});

The Grunt task

Overview

In your project's Gruntfile, add a section named jamoose to the data object passed into grunt.initConfig().

grunt.initConfig({
  jamoose: {
    options: {
      // Task-specific options go here.
    },
    your_target: {
      // Target-specific file lists and/or options go here.
    },
  },
});

Options

options.jade

Type: Object Default value: {}

Same as Jade API options

_NB: options.jade.filename defaults to the current file's path

options.juice

Type: Object Default value: { url: 'file://' + process.cwd() + '/' }

Same as Juice options

Important: End your url with a trailing slash

Usage Example

grunt.initConfig({
  jamoose: {
    default_options: {
      files: [
        {
          expand: true,
          flatten: false,
          cwd: 'test/fixtures',
          src: '**/*.jade',
          dest: 'tmp',
          ext: '.html'
        }
      ]
    }
  }
});

Using in your application

See the examples for detailed, working code.

  1. Create your email template using Jade. Use Jade variables for anything you want replaced at build time (think cross-app vars like domains and dates). Use Mustache variables for anything you want inserted at send time (think user-specific details like name or address)
//- welcome.jade
html
  head
    link(rel="stylesheet", href="path/to/email.css")
  body
    table
      tr
        td
          h1 Welcome {{name}}

      tr
        td
          a(href=domain + '/privacy') Privacy Policy
// email.sass
h1
  font-size: 30px
  color: #333
  1. Build your templates with Grunt
$ grunt sass jamoose

You now will have an HTML file with inlined CSS* and Mustache variables still in tact.

<html>
  <body>
    <table>
      <tr>
        <td>
          <h1 style="font-size:30px;color:#333">Welcome {{name}}</h1>
        </td>
      </tr>
      <tr>
        <td>
          <a href="http://your.domain.from.grunt/privacy">Privacy Policy</a>
        </td>
      </tr>
    </table>
  </body>
</html>
  1. Send an email in your app
var jamoose = require('jamoose'),
    mailer = new jamoose({
      tplPath: 'path/to/templates/from/grunt/',
      fromEmail: '[email protected]'
    });

mailer.send(
  '[email protected]', // to
  'Welcome!', // subject
  'welcome', // template
  { // data
    name: 'John Smith'
  },
  function(err) { // callback
    if (err) { console.log(err); }
  }
);

"Welcome John Smith" will be sent to "[email protected]" from "[email protected]" with the subject "Welcome!"

Email Providers

Currently supporting:

Please submit a pull request to add more service providers.

Set the environment variables for the provider of your choice and it will be used automatically.

SendGrid

SendGrid's node library is used under the hood. Required environment variables:

  • SENDGRID_USER - API user (usually email address)
  • SENDGRID_KEY - API key (usually your password)

Mandrill

Mandrill's node library is used under the hood. Required environment variable:

Using in development

If process.env.NODE_ENV is set to development, HTML files will be written to a temporary file when you send. The path is console.log for you to inspect. If you'd like to actually send an email during development or locally, set process.env.NODE_ENV to something besides development.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Testing

Tests are written with nodeunit and can be run with npm test

Release History

v2.0.0 - Drop support for Node v0.8 v1.0.0 - Add Mandrill support

License

Copyright (c) 2014 Max Beatty Licensed under the MIT license.