From a202d6e08df0e4bf4e681635a1df933b23d9f1ef Mon Sep 17 00:00:00 2001 From: Vimal Date: Tue, 2 Sep 2025 21:36:16 +0530 Subject: [PATCH] contact-Page:added complete contact page backend and frontend --- client/src/App.jsx | 2 + client/src/pages/Contact.jsx | 159 +++++++++++++++++++++++++++ server/config/emailConfig.js | 2 +- server/controllers/authController.js | 36 +++++- server/routes/authRoutes.js | 2 + 5 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 client/src/pages/Contact.jsx diff --git a/client/src/App.jsx b/client/src/App.jsx index 3cbfe4a..e88e54a 100644 --- a/client/src/App.jsx +++ b/client/src/App.jsx @@ -21,6 +21,7 @@ import AboutPage from "./pages/AboutPage"; import NotFound from "./pages/NotFound"; import FaqWidget from "./components/FaqWidget"; // ✅ merged from feat/faq +import ContactForm from "./pages/contact"; // Socket connection (change URL as needed) const socket = io("http://localhost:3000"); @@ -109,6 +110,7 @@ const App = () => { } /> } /> } /> + }/> } /> diff --git a/client/src/pages/Contact.jsx b/client/src/pages/Contact.jsx new file mode 100644 index 0000000..934a2bb --- /dev/null +++ b/client/src/pages/Contact.jsx @@ -0,0 +1,159 @@ +import React, { useState } from "react"; +import { motion } from "framer-motion"; + +const ContactForm = () => { + const [formData, setFormData] = useState({ + name: "", + email: "", + subject: "", + message: "", + }); + + const [status, setStatus] = useState(null); + + const handleChange = (e) => { + setFormData({ ...formData, [e.target.name]: e.target.value }); + }; + + const handleSubmit = async (e) => { + e.preventDefault(); + + try { + const res = await fetch("http://localhost:5000/api/auth/send", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(formData), + }); + + const data = await res.json(); // fetch ke liye yeh zaruri hai + + if (res.ok) { + setStatus({ success: true, message: data.success }); + setFormData({ + name: "", + email: "", + subject: "", + message: "", + }); + } else { + setStatus({ success: false, message: data.error || "Something went wrong!" }); + } + } catch (error) { + console.error("Error:", error); + setStatus({ + success: false, + message: "Something went wrong!", + }); + } +}; + + + return ( + <> + +
+

Contact Us -:

+ + {status && ( +

+ {status.message} +

+ )} + +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ + +
+
+ + ); +}; + +export default ContactForm; diff --git a/server/config/emailConfig.js b/server/config/emailConfig.js index 57d3167..eccb4ec 100644 --- a/server/config/emailConfig.js +++ b/server/config/emailConfig.js @@ -18,7 +18,7 @@ const sendMail = async (to, subject, text, html) => { from: `"ChatterSpace" <${process.env.EMAIL_USER}>`, to, subject, - text, + html:text, }; await transporter.sendMail(mailOptions); diff --git a/server/controllers/authController.js b/server/controllers/authController.js index 1815332..ff9f546 100644 --- a/server/controllers/authController.js +++ b/server/controllers/authController.js @@ -288,6 +288,39 @@ const resetPassword = async (req, res) => { } +//contactUs + + +const sendContactEmail = async (req, res) => { + const { name, email, subject, message } = req.body; + + if (!name || !email || !message) { + return res.status(400).json({ error: 'All fields are required' }); + } + + try { + const emailMessage = ` +

New Contact Query

+

Name: ${name}

+

Email: ${email}

+

Message:

+

${message}

+ `; + + await sendMail( + process.env.SMTP_FROM_EMAIL, + subject || 'New Query', + emailMessage + ); + + res.status(200).json({ success: 'Email sent successfully!' }); + } catch (error) { + console.error('Error in sending email:', error); + res.status(500).json({ error: 'Failed to send email' }); + } +}; + + module.exports = { signupUser, @@ -297,5 +330,6 @@ module.exports = { getuserbyid, deleteuser, forgotPassword, - resetPassword + resetPassword, + sendContactEmail } diff --git a/server/routes/authRoutes.js b/server/routes/authRoutes.js index e3940b3..18eb382 100644 --- a/server/routes/authRoutes.js +++ b/server/routes/authRoutes.js @@ -21,5 +21,7 @@ router.delete("/delete/:id",authController.deleteuser); // ⬅️ Add this new route for forgot password router.post('/forgotpassword', authController.forgotPassword); +//contactUs +router.post('/send', authController.sendContactEmail); module.exports = router;