Coder Social home page Coder Social logo

simple-template-mailer's Introduction

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.

simple-template-mailer's People

Contributors

felixfurtmayr avatar rf-alex avatar web-mi avatar

Watchers

Uli Köhler avatar James Cloos avatar  avatar Regina Galambos 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.