-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (128 loc) · 3.44 KB
/
index.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
import express from "express";
import net from "net";
import dns from "dns";
const app = express();
import cors from "cors";
import bodyParser from "body-parser";
const PORT = process.env.PORT || 3333;
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get("/", (_, res) => {
return res
.status(200)
.json({ success: true, message: "Welcome on Mailtester API" });
});
const validateEmail = (email) => {
const validSyntax = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,}$/.test(email);
if (!validSyntax) {
return Promise.resolve(false);
}
const domain = email.split("@")[1];
const ports = [25, 587, 465];
return new Promise((resolve) => {
const tryNextPort = (index) => {
if (index >= ports.length) {
resolve(false);
return;
}
const port = ports[index];
dns.resolveMx(domain, (err, mxRecords) => {
if (err || !mxRecords || mxRecords.length === 0) {
tryNextPort(index + 1);
} else {
const smtp = net.createConnection(port, mxRecords[0].exchange);
let connected = false;
const timeout = 3000; // 3 seconds
const timeoutId = setTimeout(() => {
if (!connected) {
smtp.destroy();
tryNextPort(index + 1);
}
}, timeout);
smtp.on("connect", () => {
connected = true;
clearTimeout(timeoutId);
smtp.destroy();
resolve(true);
});
smtp.on("error", () => {
tryNextPort(index + 1);
});
}
});
};
tryNextPort(0);
});
};
app.get("/single-mail/:email", async (req, res) => {
const email = req.params.email;
validateEmail(email)
.then((response) => {
if (response) {
return res.status(200).json({
success: true,
message: `${email} is a valid email address.`,
});
} else {
return res.status(406).json({
success: false,
message: `${email} is NOT a valid email address.`,
});
}
})
.catch((err) => {
return res.status(406).json({
success: false,
message: `${email} is NOT a valid email address.`,
error: err,
});
});
});
app.post("/bulk-mails", (req, res) => {
if (!req.body) {
return res.status(400).json({
success: false,
message: "Body must be provided",
});
}
if (!req.body.emails) {
return res.status(400).json({
success: false,
message: "Emails must be provided",
});
}
const emails = req.body.emails;
if (!Array.isArray(emails)) {
return res.status(400).json({
success: false,
message: "Emails must be an array",
});
}
const validEmails = [];
const invalidEmails = [];
for (let email of emails) {
validateEmail(email)
.then((response) => {
if (response) {
validEmails.push(email);
} else {
invalidEmails.push(email);
}
if(emails.length === validEmails.length + invalidEmails.length) {
return res.status(200).json({
success: true,
message: "Emails validated",
validEmails: validEmails,
invalidEmails: invalidEmails,
});
}
})
.catch(_ => {
invalidEmails.push(email);
});
}
});
app.listen(PORT, () => {
console.info(`App listening at http://localhost:${PORT}`);
});