forked from germancutraro/Contact-Form-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
86 lines (77 loc) · 2.62 KB
/
Copy pathapp.js
File metadata and controls
86 lines (77 loc) · 2.62 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
74
75
76
77
78
79
80
81
82
83
84
85
86
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const unirest = require('unirest');
const port = process.env.PORT || 3000;
app.set('view engine', 'ejs');
app.use('/public', express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use((req, res, next) => {
res.locals.msg = '';
next();
});
app.get('/', (req, res) => {
res.render('index');
});
app.post('/send', (req, res) => {
let output = `
<h1>New Email!</h1>
<ul>
<li>Name: ${req.body.name}</li>
<li>Email: ${req.body.email}</li>
<li>Subject: ${req.body.subject}</li>
<li>Message: ${req.body.message}</li>
</ul>
`;
const message = req.body.message;
const sender_ip = req.ip;
// check for spam
unirest.post("https://oopspam.p.rapidapi.com/v1/spamdetection")
.header("X-RapidAPI-Host", "oopspam.p.rapidapi.com")
.header("X-RapidAPI-Key", "YOUR_API_KEY")
.header("Content-Type", "application/json")
.send({ "sender_ip": sender_ip, "content": message })
.end(function (result) {
console.log(result.status, result.headers, result.body);
if (result.status != 200) {
// handle the error
return console.log(result.body);
}
if (result.body.Score < 3) {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'mail.YOURDOMAIN.com',
port: 587,
secure: false,
auth: {
user: 'YOUREMAIL',
pass: 'YOURPASSWORD'
},
tls: {
rejectUnauthorized: false
}
});
// setup email data with unicode symbols
let mailOptions = {
from: '"Nodemailer Contact" <your@email.com>', // sender address
to: 'RECEIVEREMAILS', // list of receivers
subject: 'Node Contact Request', // Subject line
text: 'Hello world?', // plain text body
html: output // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
res.render('contact', { msg: 'Email has been sent' });
});
}
});
});
server.listen(port, '0.0.0.0', () => console.log(`App running on port ${port}`));