-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (46 loc) · 1.47 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
const express = require('express');
const cors = require("cors");
const nodemailer = require("nodemailer");
const app = express();
app.use(cors());
app.use(express.json());
const port = process.env.PORT || 5000;
app.get("/", (req, res) => {
res.sendFile(__dirname + "/views/index.html");
});
app.post("/send", async (req, res) => {
try {
const { sender, receiver, subject, data, cc, bcc } = req.body;
// Create a transporter
let transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: sender.mail,
pass: sender.password,
}
});
const info = await transporter.sendMail({
from: `"${sender.name}" <${sender.mail}>`,
to: receiver && receiver.length > 0 ? receiver : null,
subject: subject,
html: data,
cc: cc && cc.length > 0 ? cc : null,
bcc: bcc && bcc.length > 0 ? bcc : null
});
let result = (info.accepted.length > 0) ? true : false;
if (result)
res.status(200).json({
type: "success",
message: "Mail sent successfully"
});
} catch (err) {
res.status(500).json({
type: "error",
message: err.message.includes("Invalid login") ? "Invalid sender credentials" : err.message
})
}
});
app.listen(port, () => {
console.log(`Server running`);
});
module.exports = app;