-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
87 lines (71 loc) · 2.35 KB
/
server.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
// https://www.youtube.com/watch?v=JpcLd5UrDOQ
// https://www.youtube.com/watch?v=EPnBO8HgyRU
require('dotenv').config();
// get express
const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
//const mailgun = require("mailgun-js");
const path = require('path');
const email = process.env.EMAIL;
const pass = process.env.EMAIL_PASSWORD;
// initialize express app
const app = express();
// Serve the static files from the React app
app.use(express.static(path.join(__dirname, 'client/build')));
// configure data parsing for email form
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
//const mg = mailgun({apiKey: api_key, domain: DOMAIN});
app.post('/api/form', (req, res) => {
console.log(req.body)
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: email,//replace with your email
pass: pass //replace with your password
}
});
const htmlEmail = `
<h3>Contact Details</h3>
<ul>
<li>First Name: ${req.body.firstName}</li>
<li>Last Name: ${req.body.lastName}</li>
<li>Email: ${req.body.email}</li>
<li>Phone: ${req.body.phone}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`
const data = {
from: '[email protected]',
to: '[email protected]',
subject: 'New Design Request',
text: req.body.message,
html: htmlEmail
};
// mg.messages().send(data, function (error, body) {
// console.log(body);
// });
transporter.sendMail(data, function(error, info){
if (error) {
res.status(404)
console.log(error);
res.end()
}
else {
res.status(200)
console.log('Email sent: ' + info.response);
}
});
return res.send('Email successfully sent')
});
// Handles any requests that don't match the ones above
app.get('*', (req,res) =>{
res.sendFile(path.join(__dirname+'/client/build/index.html'));
});
// port 3001 or whatever port process.env sets up for us
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`)
});