forked from Its-Aman-Yadav/Community-Site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (41 loc) · 1.32 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
const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const app = express();
// Middleware
app.use(bodyParser.urlencoded({ extended: true }));
// Routes
app.post('/send-email', (req, res) => {
const { name, email, message } = req.body;
console.log(req.body)
// Create a transporter
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'email', // Your email address
pass: 'password' // Your email password or App Password
}
});
// Email options
const mailOptions = {
from: 'email',
to: '[email protected]', // Recipient's email address
subject: 'Contact Form Submission',
text: `Name: ${name}\nEmail: ${email}\nMessage: ${message}`
};
// Send email
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error(error);
res.send('Error: Something went wrong. Please try again later.');
} else {
console.log('Email sent: ' + info.response);
res.send('Email sent successfully!');
}
});
});
// Start server
const PORT = process.env.PORT || 5500;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});