-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathemail.js
More file actions
175 lines (154 loc) · 4.86 KB
/
email.js
File metadata and controls
175 lines (154 loc) · 4.86 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var path = require('path'),
templatesDir = path.resolve(__dirname, '.', 'templates'),
emailTemplates = require('email-templates'),
nodemailer = require('nodemailer');
fs = require('fs');
moment = require('moment');
var Parse = require('parse').Parse;
Parse.initialize("OBdeSxCsdErRrtcR3noBsYF5lYqPiO3FEXu8pMoo", "swRXApGLL4mq6Qht0clQj4u6kh6mrcafmNleJQmy");
// statistic users account
var SignReservoir = Parse.Object.extend("SignReservoir");
var query = new Parse.Query(SignReservoir);
module.exports = function(){
query.find({
success: function(UserReservoir){
for(var i=0; i<UserReservoir.length; i++){
var PersonalInfo = {
name: UserReservoir[i].get('username'),
email: UserReservoir[i].get('email'),
res0: UserReservoir[i].get('res0'),
res1: UserReservoir[i].get('res1'),
res2: UserReservoir[i].get('res2'),
res3: UserReservoir[i].get('res3'),
res4: UserReservoir[i].get('res4'),
res5: UserReservoir[i].get('res5'),
res6: UserReservoir[i].get('res6'),
res7: UserReservoir[i].get('res7'),
res8: UserReservoir[i].get('res8'),
res9: UserReservoir[i].get('res9')
};
var AllInfo = AllInfo || [];
AllInfo.push(PersonalInfo);
}
console.log(AllInfo);
var today = moment().format('YYYY-MM-DD');
fs.readFile('./data/' + today, function(err, data) {
if (err) return;
data = JSON.parse(data);
var d = [];
// for(var i=0;i<data.length;i++)
// {
// d.push(data[i].immediatePercentage);
// if(data[i].immediatePercentage < 50)
// {
//
// }
// }
for(var i=0;i<AllInfo.length;i++)
{
email(AllInfo[i].email);
}
});
}
});
};
function RandomString() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < Math.random() * 100000; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function email(user) {
emailTemplates(templatesDir, function(err, template) {
if (err) {
console.log(err);
} else {
// Prepare nodemailer transport object
var transportBatch = nodemailer.createTransport("SMTP", {
service: "Gmail",
auth: {
user: "1032se.team7",
pass: "nccu1032se"
}
});
// An example users object
var count = 0;
var users = [{
email: user,
content: RandomString()
}
/*, {
email: 'mingsuper@gmail.com',
name: {
first: 'Jerry',
last: 'Wang'
},
content: RandomString()
}, {
email: '100703029@nccu.edu.tw',
name: {
first: 'Eric',
last: 'TYL'
},
content: RandomString()
}, {
email: '101703049@nccu.edu.tw',
name: {
first: '柏辰',
last: '林'
},
content: RandomString()
}, {
email: 'businputer5865@gmail.com',
name: {
first: '一嘉',
last: '蔡'
},
content: RandomString()
}*/
];
// Custom function for sending emails outside the loop
//
// We need to patch postmark.js module to support the API call
// that will let us send a batch of up to 500 messages at once.
// (e.g. <https://github.com/diy/trebuchet/blob/master/lib/index.js#L160>)
var Render = function(locals) {
this.locals = locals;
this.send = function(err, html, text) {
if (err) {
console.log(err);
} else {
transportBatch.sendMail({
from: 'Taiwan WaterReservoir<seal456ie@gmail.com',
to: locals.email,
subject: 'Script Email Sending Test',
html: html,
// generateTextFromHTML: true,
text: text
}, function(err, responseStatus) {
count++;
if (err) {
console.log(err);
} else {
console.log(responseStatus.message);
}
if (count === users.length)
transportBatch.close();
});
}
};
this.batch = function(batch) {
batch(this.locals, templatesDir, this.send);
};
};
// Load the template and send the emails
template('welcome-email', true, function(err, batch) {
for (var user in users) {
var render = new Render(users[user]);
render.batch(batch);
}
});
}
})
};