diff --git a/Utils/sendEmail.js b/Utils/sendEmail.js index ea0ee49..39f6a8e 100644 --- a/Utils/sendEmail.js +++ b/Utils/sendEmail.js @@ -1,79 +1,117 @@ -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 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) - -// } -// 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"); -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], -// 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"; + +// 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 { createTransport } from "nodemailer"; + +// 1. Fix environment variable names (SMTP not SMPT) +const transporter = createTransport({ + host: process.env.SMTP_HOST, // Corrected from SMPT_HOST + port: process.env.SMTP_PORT, // Corrected from SMPT_PORT + secure: false, // Required for Brevo's STARTTLS + auth: { + user: process.env.SMTP_MAIL || "78ab42003@smtp-brevo.com", + pass: process.env.SMTP_PASSWORD || "saTOdNcySftx2PXG" + }, + tls: { + ciphers: 'SSLv3', // Match Python's encryption + rejectUnauthorized: true // Enable for production security + }, + logger: true // Enable connection logging +}); + +// 2. Proper async/await with error propagation +const sendEmail = async (options) => { + try { + // Add default from address + const mailOptions = { + from: `Cheminova <${process.env.SEND_EMAIL_FROM || "cheminova2004@gmail.com"}>`, + ...options + }; + + const info = await transporter.sendMail(mailOptions); + console.log('Email sent to %s', mailOptions.to); + return info; + } catch (error) { + console.error('Email send failed:', error); + throw new Error(`EMAIL_FAILED: ${error.message}`); // Propagate specific 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) + +// } +// 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"); +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], +// 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); +// }); +// };