2.1.2 • Published 7 years ago

ses-template-mailer v2.1.2

Weekly downloads
3
License
Apache-2.0
Repository
github
Last release
7 years ago

AWS Simple Email Service (SES) Template Mailer

Send HTML or plain text templates through Amazon Web Services Simple Email Service (SES) using Handlebars, Pug, EJS or Underscore. This is essentially a wrapper for ses.sendEmail.

Install

npm install --save ses-template-mailer

Usage

Email = require('ses-template-mailer')
email = new Email(options, credentials);
email.send(function(error, result){});

Options

NameTypeDescription
optionsObject or ObjectOptions are the same as AWS SES sendMail with the following additions. You may also pass an array of multiple email options. Multiple emails will be sent at 90 emails per second.
options.Message.TemplateDataObjectThe data to parse the template with. This can also be set with email.templateData before calling send.
options.Message.TemplateTypeStringThe template engine to use. Must be one of the following: "handlebars", "pug", "ejs", "underscore". Handlebars is the default. This can also be set with email.templateType before calling send.
options.Message.Body.HtmlStringThe text email template. Should either be an html template string or a valid url eg: https://mybucket.s3.amazonaws.com/template.handlebars. Using an extension name like "handlebars", "pug", "ejs" or "underscore" will overwrite TemplateType.
options.Message.Body.TextStringThe text email template. Should either be a text template string or a valid url eg: https://mybucket.s3.amazonaws.com/template.handlebars. Using an extension name like "handlebars", "pug", "ejs" or "underscore" will overwrite TemplateType. If Message.Body.Text is null then a plain text email will be generated from the HTML. Note: Since Pug cannot parse plain text emails text is automatically parsed from html, be sure and nullify text property (Message.Body.Text=null) if you're using Pug.
credentialsObjectConfig options are the the same as AWS Config. If you've already set up your conf globally you can leave this null

Properties

template (String)

The text email template. Should either be an html template string or a valid url eg: https://mybucket.s3.amazonaws.com/template.handlebars. Using an extension name like "handlebars", "pug", "ejs" or "underscore" will overwrite TemplateType.

templateData (Object)

The data to parse the template with. Refer to your desired templating engine for more details.

templateType (String)

The template engine to use. Must be one of the following: "handlebars", "pug", "ejs", "underscore". Handlebars is the default.

rateLimit (Number=90)

If sending multiple emails with with send delivery will be throttled ensure your don't go over your rate limit. Default: 90.

send (Function)

Dispatch emails

Parameters

NameTypeDescription
callbackFunction A callback function. If no callback is given events are emitted.
callback.errorsErrorA collection of errors or null if there were no errors
callback.resultsObjectThe result of each send
callback.results.MessageIdStringThe message id retuned from SES
callback.results.ResponseMetadataObjectThe response meta data retuned from SES
callback.results.ResponseMetadata.RequestIdStringThe request id retuned from SES

Events

COMPLETE_EVENT

Event triggered when all emails have been processed and sent.

Parameters

NameTypeDescription
errorsErrorA collection of errors or null if there were no errors
resultsObjectThe result of each send
results.MessageIdStringThe message id retuned from SES
results.ResponseMetadataObjectThe response meta data retuned from SES
results.ResponseMetadata.RequestIdStringThe request id retuned from SES

SEND_EVENT

Event trigged each time an email is sent.

NameTypeDescription
resultObjectThe result of each send
result.MessageIdStringThe message id retuned from SES
result.ResponseMetadataObjectThe response meta data retuned from SES
result.ResponseMetadata.RequestIdStringThe request id retuned from SES

ERROR_EVENT

Event trigger when an email failed to send.

NameTypeDescription
errorErrorThe send error

Examples:

Sending One Email (with callbacks)

Email = require('../lib/email')
email = new Email(
	{
		Destination: {
			BccAddresses: ['Jack Sprat <jack-sprat@hotmail.com>'],
			CcAddresses: ['Humpty Dumpty <humpty-dumpty@yahoo.com>'],
			ToAddresses: ['Peter Pumpkineater <peter-pumpkineater@gmail.com>']
		},
		Message: {
			Subject: {
				Data: 'Pumpkin Eater'
			},
			Body: {
					Html: {
						Data: '<div>{{peter}}, {{peter}} pumpkin eater, Had a wife but couldn't keep her; <a href="{{more}}">more</a></div>'
					},
					Text: {
						Data: '{{peter}}, {{peter}} pumpkin eater, Had a wife but couldn't keep her;'
					}
			},
			TemplateData: {name:'Peter', more: 'https://en.wikipedia.org/wiki/Peter_Peter_Pumpkin_Eater'},
			TemplateType: 'handlebars'
		},
		Source: 'admin@example.com',
		ReplyToAddresses: [ 'Nobody <no-reply@example.com>'],
		ReturnPath: 'admin@example.com'
	}, {
		accessKeyId: 'my_aws_access_key',
		secretAccessKey: 'my_aws_secret_key',
		region: 'us-west-2'
	}

);

// Optional setup:
// email.template = '<div>{{peter}}, {{peter}} pumpkin eater, Had a wife but couldn't keep her; <a href="{{more}}">more</a></div>'
// email.templateData = {name:'Peter', more: 'https://en.wikipedia.org/wiki/Peter_Peter_Pumpkin_Eater'}
// email.templateType = 'handlebars'

email.send(function(err, data) {
	if (err)
		console.log(err, err.stack); // error
	else
		console.log(data); // success
}

Sending Multiple Emails (with events)

Email = require('../lib/email')
email = new Email([recipient1, recipient2, recipient3,]);
email.rateLimit = 200
email.on(Email.COMPLETE_EVENT, function(errors, results, data){
	console.log(results.length+" of "+data.length+" emails have been sent.")
});
email.on(Email.SEND_EVENT, function(result){
	console.log('email sent')
});
email.on(Email.ERROR_EVENT, function(error){
	console.log(error.stack)
});
email.send()

Tests

Make a copy of the test/conf.sample.coffee and update if necessary.

cp test/conf.sample.coffee test/conf.coffee // update conf.coffee
mocha test/email.coffee
2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.0-0

7 years ago

2.0.5

7 years ago

2.0.4

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago