-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (65 loc) · 2.17 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
var nodemailer = require('nodemailer'),
smtp = require('nodemailer-smtp-transport'),
Q = require('q'),
_ = require('underscore');
if (!process.env.SITE_EMAIL_ADDRESS || !process.env. SITE_EMAIL_PASSWD || !process.env.SMTP_HOST || !process.env.SMTP_PORT) throw new Error("Expected SMTP_USER,SMTP_PASS, SMTP_HOST and SMTP_PORT env, missing one or both.");
//Also see more node mailer transport options https://github.com/nodemailer/nodemailer
var transport = nodemailer.createTransport(smtp({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
secure: false,
ignoreTLS: true,
auth: {
user: process.env.SITE_EMAIL_ADDRESS,
pass: process.env.SITE_EMAIL_PASSWD
}
}));
var mapToResults = function (arr) {
return arr.map(function (r) {
return r.value;
});
};
var send = function (toSend, onSuccess, onFail) {
var message = toSend.message;
if (!message.to || message.to.length === 0) {
console.warn("WARNING: No emails found to send mail to.");
return;
}
var messageBase = {
from: "\"" + message.from_name + "\" <" + message.from_email + ">",
subject: message.subject ,
html: message.html
};
var sendMessages = message.to.map(function (t) {
var thisMessage = _.extend({}, messageBase, {
to: "\"" + t.name + "\" <" + t.email + ">",
replyTo: "\"" + t.name + "\" <" + t.email + ">"
});
return transport.sendMail(thisMessage);
});
Q.allSettled(sendMessages).done(function (results) {
var errors = results.filter(function (r) {
return r.state !== "fulfilled";
});
if (errors && errors.length > 0) {
console.log("Errors while sending mail.");
errors.forEach(function (e) { console.error(e); });
onFail(mapToResults(errors));
}
else {
onSuccess(mapToResults(results));
}
});
};
module.exports = {
send: send,
mandrill: {
messages: {
send: send,
sendTemplate: function (a, b, c) { }
},
templates: {
render: function (a, b, c) { }
}
}
};