diff --git a/backend/controllers/contactusController.js b/backend/controllers/contactusController.js
index 4b0c192..71b4faa 100644
--- a/backend/controllers/contactusController.js
+++ b/backend/controllers/contactusController.js
@@ -1,22 +1,45 @@
import nodemailer from "nodemailer";
+import ContactUs from "../models/Contact.js";
import "dotenv/config";
+
+// POST Controller: Handles contact form submission
export const createContactUs = async (req, res) => {
const { mail, subject, message } = req.body;
+ // Check if required fields are present and valid
+ if (!mail || !subject || !message) {
+ return res.status(400).json({
+ status: "error",
+ message: "All fields (email, subject, message) are required.",
+ });
+ }
+
+ // Validate email format using a simple regex
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
+ if (!emailRegex.test(mail)) {
+ return res.status(400).json({
+ status: "error",
+ message: "Please provide a valid email address.",
+ });
+ }
+
try {
+ // Save the contact form data to the database
+ const newContactRequest = new ContactUs({
+ mail,
+ subject,
+ message,
+ });
+ await newContactRequest.save(); // Save the document to the MongoDB database
+
+ // Send email using Nodemailer
const transporter = nodemailer.createTransport({
- service: "gmail",
- host: "smtp.gmail.com",
- port: 587,
- secure: false,
+ service: 'Gmail',
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
- tls: {
- rejectUnauthorized: false, // Disable strict SSL verification
- },
});
const mailOptions = {
@@ -27,23 +50,17 @@ export const createContactUs = async (req, res) => {
};
// Send mail with defined transport object
- transporter.sendMail(mailOptions, (error, mailOptions) => {
- if (error) {
- console.error("Error occurred: " + error.message);
- return;
- }
- });
+ await transporter.sendMail(mailOptions);
res.status(200).json({
status: "success",
message: "Your contact request has been successfully received.",
});
} catch (err) {
- console.error(`Error at transport : ${err}`);
+ console.error(`Error during processing contact form: ${err.message}`);
res.status(500).json({
status: "error",
- message:
- "There was an error sending your message. Please try again later.",
+ message: "There was an error processing your message. Please try again later.",
});
}
};
diff --git a/backend/models/Contact.js b/backend/models/Contact.js
new file mode 100644
index 0000000..c977abc
--- /dev/null
+++ b/backend/models/Contact.js
@@ -0,0 +1,29 @@
+import mongoose from 'mongoose';
+const contactUsSchema = new mongoose.Schema(
+ {
+ mail: {
+ type: String,
+ required: true,
+ match: /^[^\s@]+@[^\s@]+\.[^\s@]+$/, // Email format validation
+ },
+ subject: {
+ type: String,
+ required: true,
+ },
+ message: {
+ type: String,
+ required: true,
+ },
+ dateSubmitted: {
+ type: Date,
+ default: Date.now, // Automatically set the current date when the form is submitted
+ },
+ },
+ { timestamps: true } // Automatically add createdAt and updatedAt fields
+);
+
+// Create a model based on the schema
+ const ContactUs = mongoose.model('Contact', contactUsSchema);
+
+
+ export default ContactUs
\ No newline at end of file
diff --git a/backend/routes/contactUsRouter.js b/backend/routes/contactUsRouter.js
index 6cbc0fa..11e97f6 100644
--- a/backend/routes/contactUsRouter.js
+++ b/backend/routes/contactUsRouter.js
@@ -1,7 +1,6 @@
import express from 'express';
const router = express.Router();
-import { createContactUs } from "../controllers/contactusController.js";
+import { createContactUs} from "../controllers/contactusController.js";
router.post("/contactus", createContactUs);
-
export default router;
\ No newline at end of file
diff --git a/frontend/src/Pages/Herosection.jsx b/frontend/src/Pages/Herosection.jsx
index a6e72a0..ce4edac 100644
--- a/frontend/src/Pages/Herosection.jsx
+++ b/frontend/src/Pages/Herosection.jsx
@@ -53,6 +53,9 @@ const Herosection = () => {
const ContributorCLick = () => {
navigate("/contributor"); // Navigates to the login page
};
+ const ContactClick = () => {
+ navigate("/contactus");
+ };
return (
<>
@@ -93,8 +96,21 @@ const Herosection = () => {
*/}
-
+