-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
58 lines (48 loc) · 1.19 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
const express = require('express');
const bodyParser = require('body-parser');
const mailer = require('nodemailer');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use((request, response, next) => {
response.header('Access-Control-Allow-Origin', '*');
response.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.post('/send', (req, res) => {
const smtpTransport = mailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
auth: {
user: '[email protected]',
pass: 'your-mail-password',
},
tls: {
rejectUnauthorized: false,
},
});
var name = req.body.name;
var email = req.body.email;
var message = req.body.message;
var content = `Name: ${name}\nEmail: ${email}\nMessage: ${message}`;
const mail = {
from: name,
to: '[email protected]',
subject: 'New message from your website!',
text: content,
};
smtpTransport.sendMail(mail, (err, data) => {
if (err) {
res.json({
msg: 'fail',
});
} else {
res.json({
msg: 'success',
});
}
smtpTransport.close();
});
});
const port = process.env.PORT || 8080;
app.listen(port, () => console.log('Server started on port', port));