-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.js
71 lines (65 loc) · 1.83 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
const express = require("express");
const nodemailer = require("nodemailer");
const multiparty = require("multiparty");
require("dotenv").config();
// instantiate an express app
const app = express();
app.use('/Home', express.static('Home'))
app.use('/Activity', express.static('Activity'))
app.use('/Gallery', express.static('Gallery'))
app.use('/ReachUs', express.static('ReachUs'))
app.use('/images', express.static('images'))
app.route("/").get(function (req, res) {
res.sendFile(process.cwd() + "/Home/home.html");
});
//transporter object
const transporter = nodemailer.createTransport({
//host: "smtp.gmail.com",
//port: 25,
//secure: true,
service: 'Gmail',
auth: {
user: process.env.EMAIL,
pass: process.env.PASS,
},
});
// verify connection configuration
transporter.verify(function (error, success) {
if (error) {
console.log(error);
} else {
console.log("Server is ready to take our messages");
}
});
app.post("/send", (req, res) => {
//1.
let form = new multiparty.Form();
let data = {};
form.parse(req, function (err, fields) {
console.log(fields);
Object.keys(fields).forEach(function (property) {
data[property] = fields[property].toString();
});
//2. You can configure the object however you want
const mail = {
from: data.name,
to: process.env.EMAIL,
subject: data.subject,
text: `${data.name} <${data.email}> \n${data.message}`,
};
//3.
transporter.sendMail(mail, (err, data) => {
if (err) {
console.log(err);
res.status(500).send("Something went wrong.");
} else {
res.status(200).send("Email successfully sent to recipient!");
}
});
});
});
//port will be 5000 for testing
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}...`);
});