2.0.0 • Published 8 years ago

mfdc-email v2.0.0

Weekly downloads
8
License
MIT
Repository
github
Last release
8 years ago

MFDC-Email

This module is a thin wrapper around the nodemailer module which loads its config from the locations specified in the MFDC Node project standard layout.

Quickstart guide

If you're project is already setup with appropriate config (see Expected Config) you should just be able to call the module as follows:

var email = require('mfdc-email');

// NOTE: You have to create a new instance

email()
	.send({
		to: 'someone@somewhere.com',
		from: 'noreply@domain.com',
		subject: 'Plain email test via mfdc-email',
		text: 'Hello World',
	}, function(err, res) {
		// Do something with the result
	});

or using chainable methods:

var email = require('mfdc-email');

email()
	.to('Joe Random <joe@random.com>')
	.subject('HTML chainable method email test via mfdc-email')
	.html('<p>Hello <b>World</b></p>')
	.send(function(err, res) {
		// Do something with the result
	});

Using template views

Specifying the template property either as a key in the send() object or via the chainable .template() method, will specify the file on disk to be used when composing the email.

  • The type of email to send is determined by the file extension. .txt files are plain text and .html files are rich content.
  • All templates are rendered via Mustache with the parameters used taken from the templateParams option.
  • Mustache will automatically escape all variables. If you wish to use an unsecaped variable like a URL encase it in three levels of brackets rather than two e.g. {{{url}}}
var email = require('mfdc-email');

email()
	.to('Joe Random <joe@random.com>')
	.subject('Password Recovery')
	.template(config.root + '/views/emails/password-recovery.txt')
	.templateParams({
		name: 'Joe Random',
		signoff: 'The MFDC team',
	})
	.send();

API

send(email, callback)

Dispatch an email. This is as functionally similar to the Nodemailer send() command as possible. Use this if lower level access is required.

to, from, cc, bcc, subject, text, html()

All these methods are chainable:

var email = require('mfdc-email');

email()
	.to('someone@somewhere.com')
	.subject('something')
	.send();

init()

Reinitialize the mail transport and reset all defaults to the global values.

Expected Config

This module expects the following global.config / global.app.config variables to be specified to operate:

MethodKeyTypeDescription
Allemail.enabledBooleanTemporarily disable the sending of email. If falsy this will message to the console and not actually send anything
email.methodStringWhat transport profile to use, see init() for details
email.{to,from,subject,cc,bcc}String / ArrayDefault fields to use if unspecified
email.templateStringRead a template file and render it as the email content
Mailgunmailgun.apiKeyStringThe API key for the Mailgun profile
mailgun.domainStringThe Mailgun domain, usually something like 'acme.com' (no 'http://' prefix or Mailgun suffix)