-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (46 loc) · 1.73 KB
/
server.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
import express from 'express';
import nodemailer from 'nodemailer';
import bodyParser from 'body-parser';
import dotenv from 'dotenv';
dotenv.config();
// __dirname as a global variable is not defined in ES6 module, hence the
// four lines of code below is to fix that issue
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.listen(5500, () => console.log('Listening for requests on port 5500...'));
app.use(express.static('views'));
app.get('/', (req, res) => {
res.render('index.html', {root: __dirname});
})
app.post('/contact', async (req, res) => {
const { name, email, subject, } = req.body;
// There seems to be an issue with nodemailer making the 'from' (sender's) address
// my address, making me unable to identify the actual sender.
// Hence, I am using the message body to get the sender's name and email as shown in the next line
const message = `From ${name} - ${email}: \n ${req.body.message}`;
if(process.env.EMAIL && process.env.PASSWORD){
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL,
pass: process.env.PASSWORD,
}
});
const mailOptions = {
to: process.env.EMAIL,
subject: subject,
text: message
};
transporter.sendMail(mailOptions)
.then(info => {
console.log(info);
res.json({info});
})
.catch(err => console.err(err))
}
})