require('dotenv').config();
const express = require('express');
const { validate } = require('deep-email-validator');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(express.static('public'));
app.use(express.json());
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'contactform.html'));
});
// Function to verify reCAPTCHA
async function verifyRecaptcha(token) {
const recaptchaSecret = process.env.RECAPTCHA_SECRET_KEY;
const recaptchaUrl = `https://www.google.com/recaptcha/api/siteverify?secret=${recaptchaSecret}&response=${token}`;
const response = await fetch(recaptchaUrl, { method: 'POST' });
const result = await response.json();
return result.success;
}
// Endpoint to handle form submission and send email
app.post('/send-email', async (req, res) => {
const { name, email, subject, message, 'g-recaptcha-response': recaptchaToken } = req.body;
if (!name || !email || !subject || !message || !recaptchaToken) {
return res.status(400).json({ status: 'error', message: 'Missing required fields!' })
}
// Verify the reCAPTCHA token
const isRecaptchaValid = await verifyRecaptcha(recaptchaToken);
if (!isRecaptchaValid) {
return res.status(400).json({
status: 'error',
message: 'reCAPTCHA verification failed. Please try again.'
});
}
// Validate the email
const validationResult = await validate(email);
if (!validationResult.valid) {
return res.status(400).json({
status: 'error',
message: 'Email is not valid. Please try again!',
reason: validationResult.reason
});
}
// Email sending logic
// Create a transporter object
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
secure: false, // use false for STARTTLS; true for SSL on port 465
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
}
});
// Configure the mailoptions object
const mailOptions = {
from: process.env.EMAIL_FROM,
to: process.env.EMAIL_TO,
replyTo: email,
subject: subject,
text: `From: ${name}\nEmail:${email}\n\n${message}`
};
// Send the email
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log('Error:', error);
return res.status(500).json({ status: 'error', message: 'Failed to send email due to server error.' });
} else {
console.log('Email sent: ' + info.response);
return res.status(200).json({
status: 'success',
message: 'Email successfully sent'
});
}
});
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});