-
Notifications
You must be signed in to change notification settings - Fork 8
/
Server.js
96 lines (86 loc) · 2.44 KB
/
Server.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
95
96
/*
* File Name : Server.js
* Task : Run Server and fetch multiple emails from DB to send reminder
* Invoke all the email task at once and update DB once the email is sent
*/
/*
* Load all the required modules
*/
var async = require("async");
var http = require("http");
var nodemailer = require("nodemailer");
// This will store emails needed to send.
var listofemails = ["[email protected]","[email protected]"];
// Will store email sent successfully.
var success_email = [];
// Will store email whose sending is failed.
var failure_email = [];
var transporter;
/* Loading modules done. */
function massMailer() {
var self = this;
transporter = nodemailer.createTransport("SMTP",{
host: 'smtp.gmail.com',
port: 587,
auth: {
user: '',
pass: ''
},
tls: {rejectUnauthorized: false},
debug:true
});
// Fetch all the emails from database and push it in listofemails
self.invokeOperation();
};
/* Invoking email sending operation at once */
massMailer.prototype.invokeOperation = function() {
var self = this;
async.each(listofemails,self.SendEmail,function(){
console.log(success_email);
console.log(failure_email);
});
}
/*
* This function will be called by multiple instance.
* Each instance will contain one email ID
* After successfull email operation, it will be pushed in failed or success array.
*/
massMailer.prototype.SendEmail = function(Email,callback) {
console.log("Sending email to " + Email);
var self = this;
self.status = false;
// waterfall will go one after another
// So first email will be sent
// Callback will jump us to next function
// in that we will update DB
// Once done that instance is done.
// Once every instance is done final callback will be called.
async.waterfall([
function(callback) {
var mailOptions = {
from: '[email protected]',
to: Email,
subject: 'Hi ! This is from Async Script',
text: "Hello World !"
};
transporter.sendMail(mailOptions, function(error, info) {
if(error) {
console.log(error)
failure_email.push(Email);
} else {
self.status = true;
success_email.push(Email);
}
callback(null,self.status,Email);
});
},
function(statusCode,Email,callback) {
console.log("Will update DB here for " + Email + "With " + statusCode);
callback();
}
],function(){
//When everything is done return back to caller.
callback();
});
}
new massMailer(); //lets begin