From 0c8bcab437439b27c46135eb1608c290839eb751 Mon Sep 17 00:00:00 2001 From: ROSHAN GARG Date: Wed, 7 May 2025 15:32:19 +0530 Subject: [PATCH] code push for email --- Utils/sendEmail.js | 179 ++++++++++++++++++++++++++++++--------------- 1 file changed, 119 insertions(+), 60 deletions(-) diff --git a/Utils/sendEmail.js b/Utils/sendEmail.js index 5807f2a..7ab3887 100644 --- a/Utils/sendEmail.js +++ b/Utils/sendEmail.js @@ -1,72 +1,48 @@ -import nodeMailer from "nodemailer"; -import { createTransport } from "nodemailer"; +// import nodeMailer from "nodemailer"; +// import { createTransport } from "nodemailer"; -const transporter = createTransport({ - host: process.env.SMPT_HOST, - port: process.env.SMPT_PORT, - // service: process.env.SMPT_SERVICE, - auth: { - user: process.env.SMPT_MAIL, - pass: process.env.SMPT_PASSWORD, - }, -}); +// const transporter = createTransport({ +// host: process.env.SMPT_HOST, +// port: process.env.SMPT_PORT, +// // service: process.env.SMPT_SERVICE, +// auth: { +// user: process.env.SMPT_MAIL, +// pass: process.env.SMPT_PASSWORD, +// }, +// }); -const sendEmail = async (options) => { - await transporter.sendMail(options, function (error, info) { - if (error) { - console.log(error); - } - }); -}; -export default sendEmail; - - - - - - -// import sgMail from '@sendgrid/mail'; -// sgMail.setApiKey(process.env.SENDGRID_API_KEY) -// // console.log(process.env.SENDGRID_API_KEY) // const sendEmail = async (options) => { -// sgMail.send(options) +// await transporter.sendMail(options, function (error, info) { +// if (error) { +// console.log(error); +// } +// }); +// }; +// export default sendEmail; -// } -// export default sendEmail +// // import sgMail from '@sendgrid/mail'; +// // sgMail.setApiKey(process.env.SENDGRID_API_KEY) +// // // console.log(process.env.SENDGRID_API_KEY) +// // const sendEmail = async (options) => { +// // sgMail.send(options) -// from message bird SMS send------------------------------------ +// // } +// // export default sendEmail + +// // from message bird SMS send------------------------------------ +// // import { initClient } from "messagebird"; +// // const messagebird = initClient("p2YaqxU9uYx2F3d3dV8ywAFtk"); +// // // e7HGr3kMl6su4c79DKjNAwlLQ +// // //7oOgyzfNuwBnqMc2oK6aGfczs +// // //11yKY8EbdFJpugJzaKyAH3YaK // import { initClient } from "messagebird"; + // const messagebird = initClient("p2YaqxU9uYx2F3d3dV8ywAFtk"); -// // e7HGr3kMl6su4c79DKjNAwlLQ -// //7oOgyzfNuwBnqMc2oK6aGfczs -// //11yKY8EbdFJpugJzaKyAH3YaK -import { initClient } from "messagebird"; - -const messagebird = initClient("p2YaqxU9uYx2F3d3dV8ywAFtk"); -export const sendOtp = async (recipient, message) => { - if (!recipient || !message) { - return; - } - - const params = { - originator: "+447418314922", - recipients: [recipient], - body: message, - }; - - messagebird.messages.create(params, (err, response) => { - if (err) { - console.error("Error sending message:", err); - return; - } - // console.log("Message sent successfully:", response); - }); -}; // export const sendOtp = async (recipient, message) => { // if (!recipient || !message) { // return; // } -// console.log(recipient, message); + // const params = { // originator: "+447418314922", // recipients: [recipient], @@ -75,10 +51,93 @@ export const sendOtp = async (recipient, message) => { // messagebird.messages.create(params, (err, response) => { // if (err) { -// console.error("Error sending message-------:", err); +// console.error("Error sending message:", err); // return; // } // // console.log("Message sent successfully:", response); -// // console.log("Message details:", response, response?.recipients?.items); // }); // }; +// // export const sendOtp = async (recipient, message) => { +// // if (!recipient || !message) { +// // return; +// // } +// // console.log(recipient, message); +// // const params = { +// // originator: "+447418314922", +// // recipients: [recipient], +// // body: message, +// // }; + +// // messagebird.messages.create(params, (err, response) => { +// // if (err) { +// // console.error("Error sending message-------:", err); +// // return; +// // } +// // // console.log("Message sent successfully:", response); +// // // console.log("Message details:", response, response?.recipients?.items); +// // }); +// // }; +import nodeMailer from "nodemailer"; +import { createTransport } from "nodemailer"; + +// Keep the original environment variable names +const transporter = createTransport({ + host: process.env.SMPT_HOST, + port: parseInt(process.env.SMPT_PORT, 10), // Convert string to number + // service: process.env.SMPT_SERVICE, + auth: { + user: process.env.SMPT_MAIL, + pass: process.env.SMPT_PASSWORD, + }, + // Add timeout to avoid hanging connections + connectionTimeout: 10000, // 10 seconds + // Log transport operations if not in production + debug: process.env.NODE_ENV !== "production", +}); + +const sendEmail = async (options) => { + try { + // Log attempt to help with debugging + console.log(`Attempting to send email to ${options.to}`); + + // Use Promise-based API instead of callback for better error handling + const info = await transporter.sendMail(options); + console.log("Email sent successfully:", info.messageId); + return info; + } catch (error) { + // Enhanced error logging with more details + console.error("Failed to send email:"); + console.error("Error name:", error.name); + console.error("Error message:", error.message); + console.error("Error code:", error.code); + if (error.response) { + console.error("SMTP Response:", error.response); + } + + // Re-throw so the calling code knows the email failed + throw error; + } +}; + +export default sendEmail; + +// Leave the MessageBird implementation unchanged +import { initClient } from "messagebird"; +const messagebird = initClient("p2YaqxU9uYx2F3d3dV8ywAFtk"); +export const sendOtp = async (recipient, message) => { + if (!recipient || !message) { + return; + } + const params = { + originator: "+447418314922", + recipients: [recipient], + body: message, + }; + messagebird.messages.create(params, (err, response) => { + if (err) { + console.error("Error sending message:", err); + return; + } + // console.log("Message sent successfully:", response); + }); +};