Send emails using mailgun and plain text templates


Send emails using mailgun and plain text templates

$ npm install textmail

First create a mailer with some configuration:

var TextMail = require('textmail');
var mailer = TextMail({
    // mailgun domain here 
    // mailgun key here 
    // the folder your templates live 
    // some base variables 

Now you have a mailer - you can create 'emails' - these are functions that can be run to send the content to a recipient.

// create an email object that will render 'contact.txt' and  
// send it to 3 people with 'Contact Form Submission' as the subject 
var contact = mailer.create('/contact.ejs', 'Contact Form Submission', [
app.use('/contactsubmit', function(reqres){
    var data = {
        'title':'Contact page'
    data.timestamp = new Date().getTime();
    contact(data.email, data, function(error){
        console.log('emails sent');

The templates are ejs files - this means you can pass data from the vars:

This is an email template for <%= title %>
It can also access root vars: <%= num %>

Create a new mailer with the following options:

  • domain - your mailgun account domain
  • key - your mailgun account key
  • template_root - the folder to search for templates
  • vars - vars to render for every template

Create a new email that will be sent to each email in the to_emails array.

The template can be a path relative to the 'template_root' or can be a full path.

The template is rendered using ejs.

Send the email by running it as a function passing the from address and the extra variables to render into the template

Add an extra email (or array of emails) to the list of address the email is sent to