The contact form works seamlessly locally but encounters a 500 internal server error when deployed on the server.
Form functionality
The form allows users to submit their name, email, and message. Upon submission, the message is sent via SMTP to a Gmail email address using nodemailer. Following successful submission, the user is redirected to a success page, where a confirmation message is displayed for 5 seconds before automatically redirecting back to the main page.
All required env variables are added to the node app using cpanel.
app.js
const express = require("express");
const bodyParser = require("body-parser");
const nodemailer = require("nodemailer");
const dotenv = require("dotenv");
const path = require("path");
const rateLimit = require("express-rate-limit");
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
// Middleware to parse JSON bodies
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Serve static files
app.use("/assets", express.static(path.join(__dirname, "public", "assets")));
app.use("/css", express.static(path.join(__dirname, "public", "css")));
app.use("/js", express.static(path.join(__dirname, "public", "js")));
app.use("/images", express.static(path.join(__dirname, "public", "images")));
app.use("/fonts", express.static(path.join(__dirname, "public", "fonts")));
// Rate limiting middleware
const limiter = rateLimit({
windowMs: 60 * 60 * 1000, // 1 hour
max: 5, // limit each IP to 5 requests per windowMs
message: "Too many requests from this IP, please try again later",
});
app.use("/send-email", limiter);
// Route to handle form submission
app.post("/send-email", (req, res) => {
const { name, email, message } = req.body;
console.log("Form data received:", { name, email, message });
// Create a Nodemailer transporter
const transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: process.env.GMAIL_USER,
pass: process.env.GMAIL_PASS,
},
});
// Email message options
const mailOptions = {
from: email,
to: process.env.RECIPIENT_EMAIL,
subject: "New contact form submission",
text: `Name: ${name}nEmail: ${email}nMessage: ${message}`,
};
console.log("Attempting to send email with options:", mailOptions);
// Send email
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error("Error sending email:", error);
return res.status(500).send("Failed to send email");
}
console.log("Email sent successfully:", info.response);
// Send mail-success.html with success.js
res.sendFile(
path.join(__dirname, "public", "html", "mail-success.html"),
(err) => {
if (err) {
console.error("Error sending mail-success.html:", err);
return res.status(500).send("Failed to send success page");
}
}
);
});
});
// Serve index.html for root path
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "html", "index.html"));
});
// Serve success.js for success page
app.get("/success.js", (req, res) => {
res.sendFile(path.join(__dirname, "public", "js", "success.js"));
});
// Error-handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send("Something broke!");
});
// Start server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
HTML and CSS for form:
https://codepen.io/mepsrajput/pen/ExzKyrX
HTML, CSS and JS for success page:
https://codepen.io/mepsrajput/pen/RwmaVYp