diff --git a/backend/controllers/contactController.js b/backend/controllers/contactController.js index 5322841..65feebe 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: ` +
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!
+ + + +Best Regards,
WORDWISE Team
© ${new Date().getFullYear()} WORDWISE. All rights reserved.
+