From 70f7be8bf3738d83ad8091b6b83f1eabfac84255 Mon Sep 17 00:00:00 2001 From: Sawan Date: Thu, 7 Nov 2024 00:10:07 +0530 Subject: [PATCH] Added mail functionality to feedback form --- client/src/component/Footers/Feedback.jsx | 2 + server/.env.sample | 6 +- server/Controllers/Feedback.js | 3 + server/sendFeedbackToAdmin.js | 80 +++++++++++++++++++++++ server/sendMail.js | 6 +- 5 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 server/sendFeedbackToAdmin.js diff --git a/client/src/component/Footers/Feedback.jsx b/client/src/component/Footers/Feedback.jsx index d706813..ab0118f 100644 --- a/client/src/component/Footers/Feedback.jsx +++ b/client/src/component/Footers/Feedback.jsx @@ -38,6 +38,8 @@ function Feedback(props) { // Check if the request was successful if (response.status === 200) { // Updated to check response status properly toast.success('Feedback Sent Successfully!'); + e.target.reset(); + setRating(0); setIsSubmitting(false) } else { toast.error(result.message || 'Error in submission!'); diff --git a/server/.env.sample b/server/.env.sample index 576e136..e69fe55 100644 --- a/server/.env.sample +++ b/server/.env.sample @@ -22,4 +22,8 @@ EMAIL_USER = "" EMAIL_PASS = "" GOOGLE_CLIENT_ID = "" -JWT_SECRET = "" \ No newline at end of file +JWT_SECRET = "" + +EMAIL_ID= +PASS_KEY= +ADMIN_EMAIL_ID= \ No newline at end of file diff --git a/server/Controllers/Feedback.js b/server/Controllers/Feedback.js index 4c4269d..385b418 100644 --- a/server/Controllers/Feedback.js +++ b/server/Controllers/Feedback.js @@ -1,4 +1,5 @@ const FeedbackForm = require('../Models/feedback'); +const { sendMailToAdmin } = require('../sendFeedbackToAdmin'); const submitFeedback = async (req, res) => { // Extract data from the request body @@ -27,6 +28,8 @@ const submitFeedback = async (req, res) => { rating: Rating, }; + sendMailToAdmin(feedbackData) + try { const feed = new FeedbackForm(feedbackData); diff --git a/server/sendFeedbackToAdmin.js b/server/sendFeedbackToAdmin.js new file mode 100644 index 0000000..bcd551e --- /dev/null +++ b/server/sendFeedbackToAdmin.js @@ -0,0 +1,80 @@ +const nodemailer = require('nodemailer'); +require("dotenv").config(); + +const sendMailToAdmin = (userData) => { + const transporter = nodemailer.createTransport({ + service: "gmail", + host: "smtp.gmail.com", + port: 587, + secure: false, // Use `true` for port 465, `false` for other ports + auth: { + user: process.env.EMAIL_ID, // Your email address + pass: process.env.PASS_KEY, // Your email password or app-specific password + }, + }); + + async function main() { + await transporter.sendMail({ + from: { + name: `Bitbox Feedback - ${new Date().toLocaleString()}`, + address: process.env.EMAIL_ID, + }, + to: process.env.ADMIN_EMAIL_ID, // Admin email address + subject: "New Feedback Submission from Bitbox ✔", // Email subject + text: "Bitbox Feedback Form", // Plain text body + html: `
+

Bitbox Feedback Form Details

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FieldValue
Name${userData.name}
Email${userData.email}
Subject${userData.subject}
Date of Visit${new Date(userData.dateOfVisit).toLocaleString()}
Device Used${userData.deviceUsed}
Priority Level${userData.priorityLevel}
Suggestions${userData.suggestions}
Feedback${userData.feedback}
Rating${userData.rating}
Submitted At${new Date().toLocaleString()}
+
`, // HTML body content + }); + } + + main().catch(console.error); +}; + +module.exports = { sendMailToAdmin }; diff --git a/server/sendMail.js b/server/sendMail.js index 911f841..01ea7d5 100644 --- a/server/sendMail.js +++ b/server/sendMail.js @@ -8,8 +8,8 @@ const sendMailToAdmin = (userData) => { port: 587, secure: false, // Use `true` for port 465, `false` for other ports auth: { - user: process.env.EMAIL_USER, // Your email address - pass: process.env.EMAIL_PASS, // Your email password or app-specific password + user: process.env.EMAIL_ID, // Your email address + pass: process.env.PASS_KEY, // Your email password or app-specific password }, }); @@ -18,7 +18,7 @@ const sendMailToAdmin = (userData) => { await transporter.sendMail({ from: { name: `Bitbox Contact - ${new Date().toLocaleString()}`, - address: process.env.EMAIL_USER, + address: process.env.EMAIL_ID, }, to: process.env.ADMIN_EMAIL_ID, // Admin email address subject: "New Contact Form Submission from Bitbox ✔", // Email subject