-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.js
More file actions
119 lines (107 loc) · 4.53 KB
/
index.js
File metadata and controls
119 lines (107 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'use strict';
var path = require('path'); // -> resolve template files
var fs = require('fs'); // -> open template files
var Handlebars = require('handlebars'); // -> compile templates
var COMPILED_TEMPLATES = {}; // -> cache compiled templates
var send = require('./service').initialize(); // -> email service, for sending
/**
* set_template_directory does exactly what its name suggests
* @param {String} dir - the template directory
* @returns {undefined}
*/
function set_template_directory (dir) {
if (!dir) {
throw new Error('Please Set a Template Directory');
}
fs.readdirSync(dir); // this should be sync (on startup)
process.env.TEMPLATE_DIRECTORY = dir; // set the env var
}
set_template_directory(process.env.TEMPLATE_DIRECTORY);
/**
* open template file sync (ONCE) and compile it!
* @param {String} template_name - filename of template
* @param {String} type - the file type
* @returns {Object} - compiled templates
*/
function compile_template (template_name, type) {
var filename = template_name + '.' + type;
var template_cached = COMPILED_TEMPLATES[template_name + '.' + type];
var compiled, template, filepath;
// check if the template has already been opened
if (!template_cached) {
filepath = path.resolve(process.env.TEMPLATE_DIRECTORY, filename);
template = fs.readFileSync(filepath, 'utf8');
compiled = Handlebars.compile(template);
COMPILED_TEMPLATES[template_name + '.' + type] = compiled;
}
return COMPILED_TEMPLATES[template_name + '.' + type];
}
function isTruthy (x) {
return Boolean(x);
}
/**
* sendMany function is similar to sendemail but allows more control
* of params including multiple recipients and CC and BCC recipients
*
* @param {Object} options -
* @param {String[]} options.toAddresses - The recipient emails addresses
* @param {String[]} [options.ccAddresses] - The cc recipient emails addresses
* @param {String[]} [options.bccAddresses] - The bcc recipient emails addresses
* @param {Object} options.context - The key value pairs
* that will be interpolated in the template
* @param {String} [options.senderEmailAddress] - Specifies the sender
* email address,defaults to SENDER_EMAIL_ADDRESS environment variable
* @param {String} [options.replyToAddress] - Specifies the reply to
* email address, defaults to SENDER_EMAIL_ADDRESS environment variable
* @param {String} [options.htmlCharset] - charset for html email body
* @param {String} [options.textCharset] - charset for text email body
* @param {String} [options.subjectCharset] - charset for email subject
* @param {Function} callback - continuation function called after
* the email has been sent.
* @returns {undefined}
*/
function sendMany (options, callback) {
/* eslint-disable max-len */
var params = {
senderEmailAddress: options.senderEmailAddress || process.env.SENDER_EMAIL_ADDRESS,
subject: options.subject,
toAddresses: (options.toAddresses || []).filter(isTruthy),
ccAddresses: (options.ccAddresses || []).filter(isTruthy),
bccAddresses: (options.bccAddresses || []).filter(isTruthy),
replyToAddress: options.replyToAddress || process.env.SENDER_EMAIL_ADDRESS,
defaultCharset: 'utf8',
html_body: compile_template(options.templateName, 'html')(options.context),
txt_body: compile_template(options.templateName, 'txt')(options.context)
};
/* eslint-enable max-len */
send(params, callback);
}
/**
* sendemail method takes a template name and person object and uses
* AWS SES to send the desired email.
* @param {String} template_name - the template to use for the email
* @param {Object} person - the object containing the details of the
* person to whom we want to send the email. Requires both name
* and email. if you don't *know* the name of the person, leave it
* an empty string.
* @param {Function} callback - continuation function called after
* the email has been sent.
* @returns {undefined}
*/
function sendemail (template_name, person, callback) {
var options = {
templateName: template_name,
context: person,
subject: person.subject,
toAddresses: Array.isArray(person.email) ? person.email : [person.email],
senderEmailAddress: person.senderEmailAddress,
replyToAddress: person.replyToAddress,
ccAddresses: null,
bccAddresses: null
};
sendMany(options, callback);
}
module.exports.email = sendemail;
module.exports.sendMany = sendMany;
module.exports.compile_template = compile_template;
module.exports.set_template_directory = set_template_directory;