-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.js
104 lines (82 loc) · 2.15 KB
/
mailer.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
97
98
99
100
101
102
const express = require('express');
const app = express();
const mailer = require('nodemailer');
const kue = require('kue');
const queue = kue.createQueue();
const kueUiExpress = require('kue-ui-express');
kueUiExpress(app, '/kue/', '/kue-api/');
const cron = require('node-cron');
//mail setup
var carrier = mailer.createTransport({
service : 'gmail',
auth : {
user : '[email protected]',
pass : <my password here>
}
});
//mailing options
const mailOptions = {
from : '[email protected]',
to : null,
subject : 'Test mail',
html : '<p>Hello. This is a test mail.</p>'
};
//
// //function that sends mail.
var mailing_func = function(){
return new Promise(function(resolve, reject){
carrier.sendMail(mailOptions, function(err, info){
if(err) reject(err);
else resolve(info);
})
})
}
// //reciever lists
//function that creates jobs and invoke sendMail function
var mailman = function(){
for(var i = 0 ; i < mailing_list.length ; i++){
var job = queue.create(`'email${i}'`, {
title : "Email Sending",
to : `"${mailing_list[i]}"`
}).attempts(5).ttl(100000).priority('high').save();
job.on('complete', function(result){
console.log(result);
});
job.on('progress', function(progress, data){
console.log('job #' + job.id + ' ' + progress + '% complete.');
});
queue.process(`'email${i}'`, function(job,done){
job.log()
sendMail(job.data.to, done);
})
}
}
//
// //function that the job has to perform.
var sendMail = function(mail_id, done){
console.log(mail_id);
mailOptions.to = mail_id;
console.log("Sending mail to" + mailOptions.to);
mailing_func()
.then(function(result){
console.log(result);
done();
})
.catch(function(err){
console.log(err);
});
};
//cron
var task = cron.schedule('59 8 16 28 6 *', function(){
mailman();
}, false
);
var func = function(){
task.start();
}
//rest of the stuffs.
app.use('/kue-api/', kue.app);
//app.use(mailman);
app.use(func);
app.listen(3000);