-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
58 lines (39 loc) · 1.35 KB
/
app.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
const express = require('express');
const multer = require('multer');
const upload = multer();
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
require('dotenv').config();
const { EMAIL_USER, EMAIL_PASS, RECEIVER_EMAIL} = process.env;
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: EMAIL_USER,
pass: EMAIL_PASS
}
});
const app = express();
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/contact.html');
});
app.post('/contact', upload.none(), async (req, res) => {
const { contactFullName, contactEmail, contactPhone, contactMsgSubject, contactMessage } = req.body;
const mailOptions = {
from: contactEmail,
to: RECEIVER_EMAIL,
subject: 'New message from your website contact form',
text: `Name: ${contactFullName}\nEmail: ${contactEmail}\nPhone: ${contactPhone}\nSubject: ${contactMsgSubject}\nMessage: ${contactMessage}`
};
try {
const info = await transporter.sendMail(mailOptions);
console.log(`Email sent: ${info.response}`);
res.sendStatus(200);
} catch (error) {
console.error(error);
res.sendStatus(500);
}
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));