From 7f048306a2fd1dbd1545b75e8db6e1edecc992fc Mon Sep 17 00:00:00 2001 From: Harshit Nayan Date: Thu, 18 Sep 2025 12:55:17 +0530 Subject: [PATCH 1/2] Refactor email service to use new API endpoint and improve error handling --- .env.example | 2 +- pages/api/send-email.js | 53 ++++++++++++++++++++++++++++++++++ utils/services/emailService.js | 44 +++++++--------------------- 3 files changed, 65 insertions(+), 34 deletions(-) create mode 100644 pages/api/send-email.js diff --git a/.env.example b/.env.example index 77aca57..786769e 100644 --- a/.env.example +++ b/.env.example @@ -1 +1 @@ -NEXT_PUBLIC_BREVO_API_KEY=your_brevo_api_key_here +NEXT_PUBLIC_BREVO_API=your_brevo_api_key_here diff --git a/pages/api/send-email.js b/pages/api/send-email.js new file mode 100644 index 0000000..792dbe3 --- /dev/null +++ b/pages/api/send-email.js @@ -0,0 +1,53 @@ +import axios from "axios"; + +export default async function handler(req, res) { + if (req.method !== "POST") { + return res.status(405).json({ error: "Method not allowed" }); + } + + const { email } = req.body; + const brevo_key = process.env.NEXT_PUBLIC_BREVO_API; + + if (!brevo_key) { + return res.status(500).json({ error: "Brevo API key is not configured" }); + } + + if (!email || !validateEmail(email)) { + return res.status(400).json({ error: "Invalid email address" }); + } + + const options = { + method: "POST", + url: "https://api.brevo.com/v3/smtp/email", + headers: { + accept: "application/json", + "content-type": "application/json", + "api-key": brevo_key, + }, + data: { + sender: { + name: "Lighthouse", + email: "hello@lighthouseweb3.xyz", + }, + to: [{ email }], + templateId: 4, + tags: ["mainsite-subscription"], + }, + }; + + try { + await axios.request(options); + return res.status(200).json({ message: "Email Submitted" }); + } catch (error) { + console.error(error); + return res.status(500).json({ error: "Failed to send email" }); + } +} + +function validateEmail(email) { + return String(email) + .toLowerCase() + .match( + /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ + ); +} diff --git a/utils/services/emailService.js b/utils/services/emailService.js index e3c1736..d1941cc 100644 --- a/utils/services/emailService.js +++ b/utils/services/emailService.js @@ -5,45 +5,23 @@ const brevo_key = process.env.NEXT_PUBLIC_BREVO_API; export const sendEmail = async (email) => { try { - if (!brevo_key) { - console.error("Brevo API key is not configured"); - notify("Email service configuration error", "error"); + if (!validateEmail(email)) { + notify(`Error: Enter a valid email address`, "error"); return; } - if (validateEmail(email)) { - const options = { - method: "POST", - url: "https://api.brevo.com/v3/smtp/email", - headers: { - accept: "application/json", - "content-type": "application/json", - "api-key": brevo_key, - }, - data: { - sender: { - name: "Lighthouse", - email: "hello@lighthouseweb3.xyz", - }, - to: [{ email }], - templateId: 4, - tags: ["mainsite-subscription"], - }, - }; - - axios - .request(options) - .then(function (response) { - // - notify("Email Submitted", "success"); - }) - .catch(function (error) { - console.error(error); - }); + const response = await axios.post("/api/send-email", { email }); + if (response.status === 200) { + notify("Email Submitted", "success"); } else { - notify(`Error: Enter a valid email address`, "error"); + notify("Failed to send email", "error"); } } catch (error) { + if (error.response && error.response.data && error.response.data.error) { + notify(error.response.data.error, "error"); + } else { + notify("Failed to send email", "error"); + } console.error(error); } }; From b55bd4c299bd0a1544b4143a976112682b340d8e Mon Sep 17 00:00:00 2001 From: Harshit Nayan Date: Thu, 18 Sep 2025 13:08:48 +0530 Subject: [PATCH 2/2] Remove unused Brevo API key from email service --- utils/services/emailService.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/utils/services/emailService.js b/utils/services/emailService.js index d1941cc..b91b346 100644 --- a/utils/services/emailService.js +++ b/utils/services/emailService.js @@ -1,8 +1,6 @@ import axios from "axios"; import { notify } from "./notification"; -const brevo_key = process.env.NEXT_PUBLIC_BREVO_API; - export const sendEmail = async (email) => { try { if (!validateEmail(email)) {