forked from rezkyfm/nodejs-contact-form
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
99 lines (82 loc) · 2.84 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Import Package
const express = require('express');
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
const path = require('path');
const nodemailer = require('nodemailer');
const config = require('./config');
// Set Package
const app = express();
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
// Server Start Notification
app.listen(3000, () => console.log("Server Started on port 3000..."));
// Set Static Folder Path
app.use('/public', express.static(path.join(__dirname, 'public')));
// Get Index Page Request
app.get ('/', (req, res) => {
res.render(config.theme);
});
// Post Email Request
app.post('/send', (req, res) => {
// Email Template
const output = `
<p>You have a message</p>
<h3>Contact Details</h3>
<p>Name: ${req.body.name}</p>
<p>Email: ${req.body.email}</p>
<h3>Message</h3>
<p>${req.body.message}</p>
`;
// Alert if successfully sending email
const successAlert = `
<div class="alert alert-success alert-dismissible fade show" role="alert">
Message has been sent
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
`;
// Alert if failed to sending email
const failAlert = `
<div class="alert alert-warning alert-dismissible fade show" role="alert">
Failed to send message. Please refresh this page
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
`;
// Create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: config.host,
port: config.port,
secure: false,
auth: {
user: config.user,
pass: config.pass
},
tls:{
rejectUnauthorized:false
}
});
// Use this is you want to use Gmail SMTP
// let transporter = nodemailer.createTransport(
// `smtps://${config.user}:${config.pass}@smtp.gmail.com`
// );
// Setup email settings
let mailOptions = {
from: config.from,
to: config.to,
subject: config.subject,
html: output
};
// Send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
res.render(config.theme, {msg: failAlert});
}
res.render(config.theme, {msg: successAlert});
});
});