-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
78 lines (63 loc) · 2.28 KB
/
Copy pathapp.js
File metadata and controls
78 lines (63 loc) · 2.28 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
const express = require('express');
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
const path = require('path');
const nodemailer = require('nodemailer');
const dotenv = require('dotenv');
dotenv.config();
const app = express();
// View engine setup
app.engine('hbs', exphbs());
app.set('view engine', 'hbs');
// Static folder
app.use('/public', express.static(path.join(__dirname, 'public')));
// Body Parser Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// ROUTES
app.get('/', (req, res) => {
res.render('index');
});
app.post('/', (req, res) => {
const output = `
<p>Portfolio Contact Inquiry</p>
<h3>Contact Details</h3>
<ul>
<li>Name: ${req.body.name}</li>
<li>Email: ${req.body.email}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`;
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: process.env.MAIL_HOST,
port: process.env.MAIL_PORT,
secure: false, // true for 465, false for other ports
auth: {
user: process.env.MAIL_USERNAME, // generated ethereal user
pass: process.env.MAIL_PASSWORD // generated ethereal password
},
tls: {
rejectUnauthorized:false
}
});
// setup email data with unicode symbols
let mailOptions = {
from: '"CAS PORTFOLIO" <sillscrystal@gmail.com>', // sender address
to: "sillscrystal@gmail.com", // list of receivers
subject: "CAS Portfolio Contact Inquiry", // Subject line
text: "New Contact Request", // 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('index', {msg1: 'Your message has been sent!', msg2: 'I look forward to speaking with you in the near future.'});
});
});
app.listen(process.env.PORT || 3000, () => console.log('server started...'));