-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
73 lines (65 loc) · 1.69 KB
/
app.js
File metadata and controls
73 lines (65 loc) · 1.69 KB
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
require('dotenv').config();
let express = require("express");
let nodemailer = require("nodemailer");
let cors = require("cors");
const { getEmailContentFromDetails } = require("./bin/lib.js");
let app = express();
app.use(cors());
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
//Initialize the smtp transporter
let smtpTransport = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: 465,
secure: true,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASSWORD
}
});
//Get route to check if API is running
app.get('/', (req, res) => {
res.json({ status: 'success', message: 'API is running' });
});
//Post route that takes in the info and sends the email
app.post(
"/",
function (req, res) {
//Set up the parameters from the post data
let mailParameters = {
to: process.env.TO_EMAIL,
subject: "Bericht via contact formulier",
from: process.env.FROM_EMAIL,
replyTo: req.body.email,
html: getEmailContentFromDetails(
req.body.name,
req.body.email,
req.body.phone,
req.body.message
)
};
//Send the mail according set parameters
smtpTransport.sendMail(mailParameters, function (err, response) {
if (err) {
console.log(err);
res.status(500).json({
status: "error",
error: err
});
} else {
console.log("Message sent");
res.json({
status: "success"
});
}
});
}
);
//Starting webserver
app.listen(process.env.PORT || 3333, function (err) {
if (err) {
console.log(err);
} else {
console.log("Listening on port on " + (process.env.PORT || 3333));
}
});