-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (52 loc) · 1.33 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
const express = require("express");
const path = require("path");
const cors = require("cors");
const dotenv = require("dotenv");
const nodemailer = require("nodemailer");
dotenv.config();
const app = express();
app.use(express.json());
app.use(cors());
app.use("/public", express.static(path.join(__dirname, "public")));
const transporter = nodemailer.createTransport({
service: process.env.MAIL_SERVICE,
auth: {
user: process.env.MAIL_USER,
pass: process.env.MAIL_PASS,
},
});
transporter.verify(function (error, success) {
if (error) {
console.log(error);
} else {
console.log("Server is ready to take our messages");
}
});
// routes
app.get("/", (req, res) => {
res.json({ message: "Succes from server" });
});
app.post("/send", (req, res, next) => {
const { name, email, date, message } = req.body;
let mail = {
from: name,
to: process.env.MAIN_MAIL,
subject: email,
text: message + date,
};
transporter.sendMail(mail, (err, data) => {
if (err) {
res.json({
status: "Failed to send mail" + err.message,
onError: true
});
} else {
res.json({
status: "You just successfull send a message",
onError: false
});
}
});
});
let port = process.env.PORT || 8080;
app.listen(port, () => console.log(`Server listening on port ${port}`));