From 09f6b71215de806e36ca05afde813b52664a8c16 Mon Sep 17 00:00:00 2001 From: Ayush Date: Fri, 8 Nov 2024 21:06:56 +0530 Subject: [PATCH] added newsleeter api and template --- backend/controllers/contactController.js | 107 ++++++++++++++++++----- backend/routes/contactRoute.js | 8 +- index.html | 54 ++++++++++++ 3 files changed, 145 insertions(+), 24 deletions(-) diff --git a/backend/controllers/contactController.js b/backend/controllers/contactController.js index 53228419..65feebe5 100644 --- a/backend/controllers/contactController.js +++ b/backend/controllers/contactController.js @@ -1,30 +1,93 @@ import Contact from "../models/contact.js"; +import nodemailer from "nodemailer"; export async function saveContact(req, resp) { - try { - const { name, email, subject, message } = req.body; - - // Check if all fields are provided - if (!name || !email || !subject || !message) { - return resp.status(400).json({ message: "All fields are required." }); - } - - // Create new contact document - const newContact = new Contact({ name, email, subject, message }); - - // Save contact form data to the database - await newContact.save(); - - // Respond with success message - resp - .status(201) - .json({ message: "Contact form submitted successfully!", newContact }); - } catch (error) { - console.error("Error saving contact form:", error); - resp.status(500).json({ message: "Failed to submit contact form.", error }); + try { + const { name, email, subject, message } = req.body; + + // Check if all fields are provided + if (!name || !email || !subject || !message) { + return resp.status(400).json({ message: "All fields are required." }); } + + // Create new contact document + const newContact = new Contact({ name, email, subject, message }); + + // Save contact form data to the database + await newContact.save(); + + // Respond with success message + resp + .status(201) + .json({ message: "Contact form submitted successfully!", newContact }); + } catch (error) { + console.error("Error saving contact form:", error); + resp.status(500).json({ message: "Failed to submit contact form.", error }); + } } export async function getContact(req, resp) { - resp.send('hello contact') + resp.send("hello contact"); +} + +export async function newsletter(req, resp) { + try { + const { email } = req.body; + + if (!email) { + return resp.status(400).json({ message: "Email is required" }); + } + + const transporter = nodemailer.createTransport({ + service: "gmail", + auth: { + user: "taskmaster991@gmail.com", + pass: "kmepakzcabvztekd", + }, + }); + + const mailOptions = { + from: process.env.EMAIL_USER, + to: email, + subject: "Thank you for Subscribing to WORDWISE", + html: ` +
+ +
+

WordWise

+

Welcome to Our Newsletter

+
+ + +
+

Thank You for Subscribing!

+

Dear Subscriber,

+

We are thrilled to have you with us. Stay tuned for our latest updates, offers, and insights to keep you informed and inspired!

+ + +
+ + Discover More + +
+
+ + +
+

Best Regards,
WORDWISE Team

+

© ${new Date().getFullYear()} WORDWISE. All rights reserved.

+
+
+ `, + }; + + // Send the email + await transporter.sendMail(mailOptions); + + resp.status(200).json({ message: "Newsletter email sent successfully!" }); + } catch (error) { + console.error("Error sending newsletter:", error); + resp.status(500).json({ message: "Error sending newsletter" }); + } } diff --git a/backend/routes/contactRoute.js b/backend/routes/contactRoute.js index 1d0e76db..b0964072 100644 --- a/backend/routes/contactRoute.js +++ b/backend/routes/contactRoute.js @@ -1,8 +1,12 @@ import express from "express"; const router = express.Router(); -import { getContact, saveContact } from "../controllers/contactController.js"; +import { + getContact, + saveContact, + newsletter, +} from "../controllers/contactController.js"; router.post("/saveContact", saveContact); router.get("/saveContact", getContact); - +router.post("/newsletter", newsletter); export default router; diff --git a/index.html b/index.html index d25ee611..56838b59 100644 --- a/index.html +++ b/index.html @@ -1555,7 +1555,61 @@

Subscribe to Our Newsletter

+