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

simple-template-mailer

v0.4.0

Published

Simple manage templates and translations, bulid and send mails. For medium projects.

Downloads

41

Readme

simple-template-mailer

Simple manage templates and translations, bulid and send mails. For medium projects. Uses "mustache" to compile html, "inline" for embedding external referneces (css, js, images) into html and nodemailer for sending. Adds a plain text version to every sent html mail.

ToDo

  • test the examples
  • testing

Installation

npm install simple-template-mailer

Example


// init the module
var simpleTemplateMailer = require('simple-template-mailer');

var mailer = simpleTemplateMailer({ // create a instance
  transporter:  { // nodemailer tramporter options; parameters should be fetched from an external config
      host: 'smtp.test.mail.address',
      requiresAuth: false,
  },
  translationsPath: __dirname +  "/translations", // template options
  templatesPath: __dirname + "/templates",
  defaultLanguage: "de",
  inlineAttribute: "inline" // default: false => inline everything; set to "inline" to only inline tags with attribute "inline"
});


// usage
mailer.send(
   // template
   {
      name: 'newsletter',
      language: "en",
      data: {test: 234} // data from your app inserted in template
      inlineAttribute: "inline" // optional: individual inline configuration
      short: false // optional: set "true" to read a short version of the template under template-short.txt
   },
   // nodemailer options
   {to:["[email protected]"]}
   // callback function
   function (err, info){
      console.log(err, info);
   });

Corresponding translation file

{
    "newsletter": "this is the subject for template 'newsletter'; mustache can be used here too: {{jsonMessage}}",
    "jsonMessage": "hello, I'm a message from the translation file" ,
    "footer": "<div class='footer'><div>"
}

Corresponding template file

<html>

<head>
   <!-- inline includes external files like css, js, img -->
    <link rel="stylesheet" href="../global.css">
    <link rel="stylesheet" href="style.css">
</head>
<body>

<img src="img.png">

<h1>simpleTemplateMailer newsletter message<h1>

{{lang.jsonMessage}}

<!-- mustache also allows html templating from the json files; use three "{" to compile html -->
{{{footer}}}

</body>
</html>

Folder structure in your project

Note: The translations folder holds all translations for the mails + the subject messages (json key has the same name, as the template).


mail
  |__translations (for mustache, and also subjects)
  |      |_de.json
  |      |_en.json
  |      |_fr.json
  |
  |__templates
       |
       |_ global.css (this css can be embedded everywhere using "inline")
       |
       |_ newsletter
       |  |_ template.html
       |  |_ img.png (css embedded with "inline")
       |  |_ style.css (img embedded with "inline")
       |  |_ template-short.txt
       |
       |_ orderconfirm
          |_ template.html
          |_ styles.css

Get compiled HTML templates

Can be used to send a compiled default mail text to the user, that can modified some passages before the text will be sent.

mailer.getTemplate({ // template options
    name: 'newsletter', // template folder name
    language: "de", // select json translation file
    data: { // data from your app for mustache
      testData: "HelloWorld"
    }
  },
  function(err, template) {
      console.log(err, template);
  }
);

Use Nodemailer direct

mailer.send(null, {to:["[email protected]"], text:"HelloWorld"});

Usage of nodemailer and inline

https://nodemailer.com/message/

https://www.npmjs.com/package/inline-source

License

ISC. Feel free to use.