forked from Its-Aman-Yadav/Community-Site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (56 loc) · 1.9 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
const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const app = express();
// subscribe-email-validation
document.querySelector('.subscribe__btn').addEventListener('click', function(event) {
validateEmail();
});
function validateEmail() {
const emailInput = document.querySelector('.subscribe__input');
const emailError = document.getElementById('subscribe_email_error');
const emailValue = emailInput.value;
const emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,}$/;
if (emailRegex.test(emailValue)) {
emailError.style.display = 'none';
} else {
emailError.style.display = 'inline';
}
}
// 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}`);
});