-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
168 lines (153 loc) · 4.53 KB
/
app.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
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
// Packages
const express = require("express");
const multer = require("multer");
const validator = require("node-email-validation");
const dotenv = require("dotenv");
const { google } = require("googleapis");
const nodemailer = require("nodemailer");
// Creating package objects
const app = express();
const upload = multer();
const OAuth2 = google.auth.OAuth2;
dotenv.config();
app.use(express.static("public"));
//setting up body-parser
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
//static arrays
let valid = [];
let invalid = [];
//GET Req
app.get("/", function (req, res) {
res.sendFile("/index.html");
});
app.post("/upload", upload.single("file"), function (req, res) {
const file = req.file;
const data = [];
//reset valid and invalid arrays
valid = [];
invalid = [];
//manual parsing of csv file instead of using csv-parser
const lines = file.buffer.toString().split("\n");
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const values = line.split(",");
data.push(values);
}
//email validation system
for (let i = 1; i < data.length; i++) {
let email = data[i][0].trimEnd();
let verdict = validator.is_email_valid(email);
if (verdict) {
valid.push(email);
} else {
invalid.push(email);
}
}
//creating HTML Table
let htmlTable =
"<body style='text-align:center;background-color: #fffbac;font-family: Helvetica, Arial, sans-serif;'>";
htmlTable +=
"<h1 class='heading' style='font-size:70px;'>Emails</h1><a href='/email'><button type='button'>Continue</button></a>";
htmlTable +=
"<div style=' display:flex;flex-direction:column;align-items:center;margin:30px;'><table style='text-align:center;padding:40px;'><tr><td><h2>Valid Emails : " +
valid.length +
"<h2></td><td><h2 style='padding:10px;'>Invaild Emails : " +
(invalid.length - 1) +
"</h2></td></tr>";
for (let i = 0; i < valid.length; i++) {
htmlTable += "<tr><td style='padding:10px;'>";
htmlTable += valid[i];
htmlTable += "</td><td>";
if (invalid[i] != undefined) {
htmlTable += invalid[i];
}
htmlTable += "</td></tr>";
}
htmlTable += "</table></div></body>";
res.send(htmlTable);
});
app.get("/email", function (req, res) {
res.sendFile(__dirname + "/email.html");
});
app.post("/email", async function (req, res) {
// const email = req.body.email;
const sub = req.body.subject;
const text = req.body.text;
for (let i = 0; i < valid.length; i++) {
const email = valid[i];
const createTransporter = async () => {
// 1
const oauth2Client = new OAuth2(
process.env.CLIENT_ID,
process.env.CLIENT_SECRET,
"https://developers.google.com/oauthplayground"
);
// 2
oauth2Client.setCredentials({
refresh_token: process.env.REFRESH_TOKEN,
});
const accessToken = await new Promise((resolve, reject) => {
oauth2Client.getAccessToken((err, token) => {
if (err) {
reject("Failed to create access token :( " + err);
}
resolve(token);
});
});
// 3
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: process.env.EMAIL_IS,
accessToken,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
refreshToken: process.env.REFRESH_TOKEN,
},
});
// 4
return transporter;
};
let transporter = nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: process.env.EMAIL_IS,
pass: process.env.PASS_IS,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
refreshToken: process.env.REFRESH_TOKEN,
},
});
// e-mail option
let mailOptions = {
from: "helixjoe13927@gmail.com",
to: email,
subject: sub,
text: text,
};
try {
// Get response from the createTransport
let emailTransporter = await createTransporter();
// Send email
emailTransporter.sendMail(mailOptions, function (error, info) {
if (error) {
// failed block
console.log(error);
} else {
// Success block
return console.log("Email sent: " + info.response);
}
});
} catch (error) {
return console.log(error);
}
}
res.sendFile(__dirname + "/success.html");
});
//PORT
app.listen(3000, function () {
console.log("Server started");
});