-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (53 loc) · 1.85 KB
/
index.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 dotenv = require('dotenv');
const path = require('path'); // Import the path module
const nodemailer = require("nodemailer");
const asyncHandler = require('express-async-handler');
const port = process.env.PORT || 9999;
const getHTML = require('./getHTML.js')
dotenv.config();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// Construct absolute path to 'index.html'
const indexPath = path.join(__dirname, 'index.html');
const sendEmail = asyncHandler(async (req, res) => {
try {
const { email, name } = req.body;
console.log(email, name);
// if(email==="[email protected]" || email==="[email protected]")
const transporter = nodemailer.createTransport({
host: 'sbg106.truehost.cloud',
port: 465,
secure: true,
auth: {
user: '[email protected]',
pass: 'W8*3Zlv]xBs2R3'
},
tls: {
rejectUnauthorized: false
}
});
const mailOptions = {
from: 'Musunza <[email protected]>', // Sender name and email
to: email,
subject: 'Elevate Your Beauty Parlour with a Stunning Website!',
html: `${getHTML(name)}` // Enhance email content as needed
};
try {
await transporter.sendMail(mailOptions);
console.log('Email sent');
res.status(200).json(mailOptions);
return `Email sent Successfully to ${email}`;
} catch (error) {
console.error('Error sending email:', error);
res.status(500).json({ message: 'Failed to send email' });
}
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Server error' });
}
});
app.get('/', (req, res) => res.sendFile(indexPath));
app.post('/', sendEmail)
app.listen(port, () => console.log(`Server started on port ${port}`));