This repository has been archived by the owner on Mar 8, 2018. It is now read-only.
/
index.js
94 lines (84 loc) · 2.06 KB
/
index.js
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
'use strict';
exports = module.exports = function(req, res, options) {
/* options = {
from: String,
to: String,
cc: String,
bcc: String,
text: String,
textPath String,
html: String,
htmlPath: String,
attachments: [String],
success: Function,
error: Function
} */
var renderText = function(callback) {
res.render(options.textPath, options.locals, function(err, text) {
if (err) {
callback(err, null);
}
else {
options.text = text;
return callback(null, 'done');
}
});
};
var renderHtml = function(callback) {
res.render(options.htmlPath, options.locals, function(err, html) {
if (err) {
callback(err, null);
}
else {
options.html = html;
return callback(null, 'done');
}
});
};
var renderers = [];
if (options.textPath) {
renderers.push(renderText);
}
if (options.htmlPath) {
renderers.push(renderHtml);
}
require('async').parallel(
renderers,
function(err, results){
if (err) {
options.error('Email template render failed. '+ err);
return;
}
var attachments = [];
if (options.html) {
attachments.push({ data: options.html, alternative: true });
}
if (options.attachments) {
for (var i = 0 ; i < options.attachments.length ; i++) {
attachments.push(options.attachments[i]);
}
}
var emailjs = require('emailjs/email');
var emailer = emailjs.server.connect( req.app.config.smtp.credentials );
emailer.send({
from: options.from,
to: options.to,
'reply-to': options.replyTo || options.from,
cc: options.cc,
bcc: options.bcc,
subject: options.subject,
text: options.text,
attachment: attachments
}, function(err, message) {
if (err) {
options.error('Email failed to send. '+ err);
return;
}
else {
options.success(message);
return;
}
});
}
);
};