-
Notifications
You must be signed in to change notification settings - Fork 2
/
bulkmailer.js
52 lines (52 loc) · 1.42 KB
/
bulkmailer.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
/**
* Created by deepak.m.shrma@gmail.com on 2/7/14.
*/
var nodemailer = require("nodemailer");
var async = require("async");
function BulkMailer(config) {
"use strict";
if (!config.transport) {
throw "transport not found!";
}
this.nodemailer = nodemailer.createTransport(config.transport);
this.isVerbose = config.verbose || false;
}
BulkMailer.prototype.send = function (info, single, callback) {
"use strict";
var that = this;
var receivers = [];
if (single) {
receivers = info.to.split(",");
} else {
receivers = [info.to];
}
var tasks = [];
async.each(receivers, function (user, eachCB) {
tasks.push(function (callback) {
info.to = user;
that.nodemailer.sendMail(info, callback);
});
eachCB();
}, function (error) {
if (!error) {
async.parallel(tasks, function (error, results) {
if (error) {
throw error;
}
else {
if (that.isVerbose) {
console.info('Mail sent to respective users!');
}
return callback(null, results);
}
});
}
else {
if (that.isVerbose) {
console.error(error);
}
return callback(error);
}
});
};
module.exports = BulkMailer;