diff --git a/package.json b/package.json index 2a2ac0d..711e805 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "mongoose": "^8.2.1", "next": "^14.1.0", "next-sitemap": "^4.2.3", + "node-mailer": "^0.1.1", "react": "^18", "react-confetti": "^6.1.0", "react-dom": "^18", diff --git a/pages/api/v1/contact/index.js b/pages/api/v1/contact/index.js index ef5ea1a..f23d663 100644 --- a/pages/api/v1/contact/index.js +++ b/pages/api/v1/contact/index.js @@ -1,30 +1,50 @@ -import Contact from "@/utils/models/contact.model"; -import DBInstance from "@/utils/db"; -DBInstance(); +import { sendmail } from "./nodemailer"; + +const escapeHTML = (str) => { + return str.replace(/[&<>"']/g, (char) => { + return { + '&': '&', + '<': '<', + '>': '>', + '"': '"', + "'": ''', + }[char]; + }); +}; export default async function handler(req, res) { if (req.method === "POST") { try { - const { name, email, message } = req.body; + let { name, email, message } = req.body; + + if (!name || !email || !message) { + return res + .status(400) + .json({ message: "All fields are required" }); + } + + name = escapeHTML(name); + email = escapeHTML(email); + message = escapeHTML(message); - const contact = new Contact({ + console.log("Received contact form data:", { name, email, message }); - await contact.save(); + const emailResponse = await sendmail(name, email, message); - res.status(200).json({ message: "Message sent successfully" }); + if (emailResponse.success) { + return res.status(200).json({ message: emailResponse.message }); + } else { + return res.status(500).json({ message: emailResponse.message }); + } } catch (error) { - console.error(error, res, "INTERNAL_SERVER_ERROR"); + console.error("Error processing request:", error); + return res.status(500).json({ message: "Internal server error" }); } } else { - console.log("🚫", req.method, "was called and got an error!"); - res.status(405).json({ - success: false, - data: null, - message: "🚫 HTTP Method not Allowed" - }); + res.status(405).json({ message: "Method not allowed" }); } } diff --git a/pages/api/v1/contact/nodemailer.js b/pages/api/v1/contact/nodemailer.js new file mode 100644 index 0000000..c73b2a2 --- /dev/null +++ b/pages/api/v1/contact/nodemailer.js @@ -0,0 +1,34 @@ +import nodemailer from "nodemailer"; + +export const sendmail = async (name, email, query) => { + try { + const transporter = nodemailer.createTransport({ + host: "smtp.zoho.in", + port: 465, + secure: true, + auth: { + user: process.env.SENDER_EMAIL, // Club Query Zoho email + pass: process.env.SENDER_PASS + } + }); + + const mailOptions = { + from: process.env.SENDER_EMAIL, + to: process.env.RECIPIENT_EMAIL, // Club community email address + subject: `Query from ${email}`, + html: `Hello Support Team,

+ Please find below a new inquiry from
Name: ${name}
Email: ${email}.

+ Query:
+ ${query}

+ Kind regards,
+ ${name}` + }; + + const info = await transporter.sendMail(mailOptions); + console.log("Email sent successfully:", info.response); + return { success: true, message: "Email sent successfully" }; + } catch (err) { + console.error("Error sending email:", err.message); + return { success: false, message: err.message }; + } +}; \ No newline at end of file diff --git a/public/sitemap-0.xml b/public/sitemap-0.xml index bc9279c..93ee0a2 100644 --- a/public/sitemap-0.xml +++ b/public/sitemap-0.xml @@ -1,10 +1,10 @@ -https://githubsrmist.tech2024-11-08T19:21:29.955Zdaily0.7 -https://githubsrmist.tech/about2024-11-08T19:21:29.955Zdaily0.7 -https://githubsrmist.tech/contact2024-11-08T19:21:29.955Zdaily0.7 -https://githubsrmist.tech/events2024-11-08T19:21:29.955Zdaily0.7 -https://githubsrmist.tech/recruitment2024-11-08T19:21:29.955Zdaily0.7 -https://githubsrmist.tech/recruitment/dashboard2024-11-08T19:21:29.955Zdaily0.7 -https://githubsrmist.tech/team2024-11-08T19:21:29.955Zdaily0.7 +https://githubsrmist.tech2024-11-14T06:38:04.705Zdaily0.7 +https://githubsrmist.tech/about2024-11-14T06:38:04.707Zdaily0.7 +https://githubsrmist.tech/contact2024-11-14T06:38:04.707Zdaily0.7 +https://githubsrmist.tech/events2024-11-14T06:38:04.707Zdaily0.7 +https://githubsrmist.tech/recruitment2024-11-14T06:38:04.707Zdaily0.7 +https://githubsrmist.tech/recruitment/dashboard2024-11-14T06:38:04.707Zdaily0.7 +https://githubsrmist.tech/team2024-11-14T06:38:04.707Zdaily0.7 \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 135ac22..889789a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4480,11 +4480,23 @@ next@^14.1.0: "@next/swc-win32-ia32-msvc" "14.2.5" "@next/swc-win32-x64-msvc" "14.2.5" +node-mailer@^0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/node-mailer/-/node-mailer-0.1.1.tgz#609e14a9b2c34422540b3a66938163d1d2421500" + integrity sha512-L3YwTtPodsYr1sNPW/PxXw0rSOr/ldygaIph2YtXDwLGt9l8km/OjM0Wrr57Yf07JEEnDb3wApjhVdR0k5v0kw== + dependencies: + nodemailer ">= 0.1.15" + node-releases@^2.0.18: version "2.0.18" resolved "https://registry.npmjs.org/node-releases/-/node-releases-2.0.18.tgz" integrity sha512-d9VeXT4SJ7ZeOqGX6R5EM022wpL+eWPooLI+5UpWn2jCT1aosUQEhQP214x33Wkwx3JQMvIm+tIoVOdodFS40g== +"nodemailer@>= 0.1.15": + version "6.9.16" + resolved "https://registry.yarnpkg.com/nodemailer/-/nodemailer-6.9.16.tgz#3ebdf6c6f477c571c0facb0727b33892635e0b8b" + integrity sha512-psAuZdTIRN08HKVd/E8ObdV6NO7NTBY3KsC30F7M4H1OnmLCUNaS56FpYxyb26zWLSyYF9Ozch9KYHhHegsiOQ== + normalize-path@^3.0.0, normalize-path@~3.0.0: version "3.0.0" resolved "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz"