-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
65 lines (55 loc) · 1.86 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
59
60
61
62
63
64
const express = require('express');
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
const path = require('path');
const nodemailer = require('nodemailer');
const app = express();
//View engine setup
app.engine('handlebars', exphbs())
app.set('view engine', 'handlebars')
app.get('/', (req, res) => {
res.render('contact')
})
//Body parser
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.listen(3000, () => console.log('Server started'))
//Static folder
app.use('/public', express.static(path.join(__dirname, 'public')))
app.post('/send', (req, res) => {
const output = `
<p>You have a new contact request</p>
<h3>Contact details</h3>
<ul>
<li>Name: ${req.body.name}</li>
<li>Name: ${req.body.email}</li>
<li>Name: ${req.body.message}</li>
</ul>
`;
let transporter = nodemailer.createTransport({
host: '',
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: '', // generated ethereal user
pass: '' // generated ethereal password
}
});
// setup email data with unicode symbols
let mailOptions = {
from: '"Codenamics 👻" <[email protected]>', // sender address
to: '[email protected]', // list of receivers
subject: 'Hello ✔', // 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: 'Message sent' })
});
})