-
-
Notifications
You must be signed in to change notification settings - Fork 707
/
Copy pathsendEmail.js
49 lines (39 loc) · 1.21 KB
/
sendEmail.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
var util = require("util");
var events = require("events");
var MailComposer = require("mailcomposer").MailComposer;
var simplesmtp = require("simplesmtp");
var SMTP_LISTEN_PORT = parseInt(process.env.SMTP_LISTEN_PORT, 10) || 30025;
var pool = simplesmtp.createClientPool(SMTP_LISTEN_PORT);
function SendEmail() {
events.EventEmitter.call(this);
}
util.inherits(SendEmail, events.EventEmitter);
SendEmail.prototype.command = function(message, timeout, cb) {
var self = this;
if (timeout instanceof Function) {
cb = timeout;
timeout = undefined;
}
timeout = timeout || 10000;
var timeoutHandle = setTimeout(function() {
console.log("sendEmail timed out.");
// if we have a callback, call it right before the complete event
if (cb) {
cb.call(self.client.api, new Error("Timed out while trying to send email"));
}
pool.close();
self.emit("complete");
}, timeout);
var mailcomposer = new MailComposer();
mailcomposer.setMessageOption(message);
pool.sendMail(mailcomposer, function (err) {
clearTimeout(timeoutHandle);
if (cb) {
cb.call(self.client.api, err);
}
pool.close();
self.emit("complete");
});
return this;
};
module.exports = SendEmail;