diff --git a/backend/.env.example b/backend/.env.example index 61078343..75cb8e17 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -2,4 +2,6 @@ MONGO_URI=mongodb://localhost:27017/bloggingdb JWT_SECRET=scijyasfy7dsvegdffvbfbfgg435tgrsnbgfgn PORT=5000 - +EMAIL_ID= +PASS_KEY= +ADMIN_EMAIL_ID= diff --git a/backend/app.js b/backend/app.js index 0ecefb29..7062278e 100644 --- a/backend/app.js +++ b/backend/app.js @@ -8,6 +8,7 @@ import contactRoutes from "./routes/contactRoute.js"; import ratingRoutes from "./routes/ratingRoutes.js"; import getInTouch from "./routes/getInTouchRoutes.js"; import addBlog from "./routes/addBlogRoutes.js"; +import subscribe from "./routes/subscribeRoutes.js"; import cors from "cors"; import path from "path"; // Import path module import { fileURLToPath } from "url"; // Import fileURLToPath @@ -35,6 +36,7 @@ app.use("/api/rating", ratingRoutes); app.use("/api/contact", contactRoutes); app.use("/api/getInTouch", getInTouch); app.use("/api/addBlog", addBlog); +app.use("/api/newsletter", subscribe); const PORT = process.env.PORT || 5000; app.listen(PORT, () => { diff --git a/backend/controllers/feedController.js b/backend/controllers/feedController.js index f9942d68..6647ceea 100644 --- a/backend/controllers/feedController.js +++ b/backend/controllers/feedController.js @@ -1,4 +1,5 @@ import Feedback from "../models/feedback.js"; +import { sendMailToAdmin } from "../sendFeedbackToAdmin.js"; // Controller to handle feedback submission export async function submitFeedback(req, res) { // Changed resp to res @@ -33,7 +34,7 @@ export async function submitFeedback(req, res) { // Changed resp to res // Save the feedback data to MongoDB await feedback.save(); - + sendMailToAdmin(feedback) // Respond with a success message res.status(201).json({ message: 'Feedback submitted successfully', feedback }); } catch (error) { diff --git a/backend/controllers/subscribeController.js b/backend/controllers/subscribeController.js new file mode 100644 index 00000000..a843490d --- /dev/null +++ b/backend/controllers/subscribeController.js @@ -0,0 +1,26 @@ +import Suscribe from "../models/subscribe.js"; +import { sendMailToSubscriber } from "../utils/sendMailToSubscribe.js"; +export async function saveSubsribe(req, res) { + try { + const { name, email } = req.body; + + if (!name || !email) { + return res.status(400).json({ message: "All fields are required." }); + } + + // Create new contact document + const newSuscribe = new Suscribe({ name, email }); + sendMailToSubscriber(newSuscribe) + await newSuscribe.save(); + res + .status(201) + .json({ message: "Contact form submitted successfully!", newSuscribe }); + } catch (error) { + console.error("Error saving contact form:", error); + res.status(500).json({ message: "Failed to submit contact form.", error }); + } +} + +export async function getSubscribe(req, res) { + res.send('hello subscriber') +} diff --git a/backend/models/subscribe.js b/backend/models/subscribe.js new file mode 100644 index 00000000..412c4aae --- /dev/null +++ b/backend/models/subscribe.js @@ -0,0 +1,23 @@ +import mongoose from "mongoose"; + +const subscribeSchema = new mongoose.Schema({ + name: { + type: String, + required: true, + trim: true + }, + email: { + type: String, + required: true, + trim: true, + match: [/^\S+@\S+\.\S+$/, "Please enter a valid email address"] + }, + subscribedAt: { + type: Date, + default: Date.now + } +}); + +const Subscribe = mongoose.model("Subscribe", subscribeSchema); + +export default Subscribe; diff --git a/backend/routes/subscribeRoutes.js b/backend/routes/subscribeRoutes.js new file mode 100644 index 00000000..2b3f839c --- /dev/null +++ b/backend/routes/subscribeRoutes.js @@ -0,0 +1,8 @@ +import express from "express"; +const router = express.Router(); +import { getSubscribe, saveSubsribe } from "../controllers/subscribeController.js"; + +router.post("/subscribe", saveSubsribe); +router.get("/getSubscribe", getSubscribe); + +export default router; diff --git a/backend/sendFeedbackToAdmin.js b/backend/sendFeedbackToAdmin.js new file mode 100644 index 00000000..349dfbe6 --- /dev/null +++ b/backend/sendFeedbackToAdmin.js @@ -0,0 +1,85 @@ +import nodemailer from 'nodemailer'; +import dotenv from 'dotenv'; + +// Load environment variables from .env file +dotenv.config(); + +const sendMailToAdmin = (userdata) => { + const transporter = nodemailer.createTransport({ + service: "gmail", + host: "smtp.gmail.com", + port: 587, + secure: false, + auth: { + user: process.env.EMAIL_ID, // by this email id you will get mail + pass: process.env.PASS_KEY, // passkey + }, + }); + + async function main() { + await transporter.sendMail({ + from: { + name: `Wordwise - ${new Date().toLocaleString()}`, + address: process.env.EMAIL_ID, + }, // sender address + to: process.env.ADMIN_EMAIL_ID, // list of receivers + subject: "New User Feedback from Wordwise ✔", // Subject line + text: "Wordwise User Feedback", // plain text body + html: `
+ Field + | ++ Value + | +
---|---|
Submitted At | +${new Date(userdata.submittedAt).toLocaleString()} | +
Overall Experience | +${userdata.overallExperience} | +
Recommendation | +${userdata.recommendation} | +
Additional Comments | +${userdata.additionalComments} | +
Improvement | +${userdata.improvement} | +
Most Helpful Feature | +${userdata.mostHelpfulFeature} | +
New Features | +${userdata.newFeatures} | +
Features Used | +${userdata.featuresUsed.join(', ')} | +
+ We’re thrilled to have you join the WordWise community—a place where words come alive, and every blog post is crafted to expand your vocabulary and deepen your understanding of language and topics you care about. +
++ At WordWise, we believe in the power of words to enlighten and inspire. Our responsive, user-friendly platform is designed with you in mind, ensuring that each visit feels as seamless as it is engaging. Whether you’re here to explore our latest blogs, delve into specific topics, or simply enjoy a well-crafted read, WordWise has something for everyone. +
++ As part of our community, you’ll be among the first to receive fresh content that’s both insightful and enriching. From curated articles that explore a variety of subjects to interactive features that enhance your reading experience, WordWise is more than just a blog—it’s a journey into the world of words. +
++ We encourage you to dive into our sections, such as Home, Leading Blogs, About, and Contact Us. Each one is thoughtfully designed to guide you through your reading adventure. And if you ever wish to share feedback or connect with us, our Contact Us page is always open. +
++ Thank you for subscribing to WordWise! We’re excited to share our latest blogs and updates with you. Here’s to many engaging reads and enriching experiences ahead! +
+
+ With warm regards,
+ The WordWise Team
+