-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
55 lines (47 loc) · 1.52 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
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3001;
const nodemailer = require('nodemailer')
require('dotenv').config();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, 'build')));
}
// Catch all route to redirect unmanaged routes back to the react app.
// THIS OPTION IS NOT SEO OPTIMIZED
// THIS ROUTE SENDS ALL SAVED LINKS TO THE ROOT
app.get('/*', function (req, res) {
res.redirect('/')
});
// CONNECTION NOT YET TESTED
// Nodemailer config to handle contact form submit.
let transporter = nodemailer.createTransport({
host: "smtp-relay.sendinblue.com",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});
// Post route for contact form submission
app.post('/send', (req, res) => {
console.log(req.body)
var { name, email, message } = req.body;
let mailOptions = {
from: `"${name}" <${email}>`, //'"Fred Foo 👻" <[email protected]>', sender address
to: `${process.env.EMAIL}`, // list of receivers
subject: "Atlas Pet Contact Form",
text: `${message}`, // plain text body
}
transporter.sendMail(mailOptions, function (err, data) {
if (err) {
console.log('Error: ', err)
} else {
console.log('Email Sent')
}
});
res.json('Email Sent');
});
app.listen(PORT, () => { console.log(`API Server now listening on PORT ${PORT}!`) })