Update Utils/sendEmail.js
This commit is contained in:
parent
50fe2b5827
commit
65ab5cbb3a
@ -1,23 +1,61 @@
|
|||||||
import nodeMailer 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 sendEmail = async (options) => {
|
||||||
|
// await transporter.sendMail(options, function (error, info) {
|
||||||
|
// if (error) {
|
||||||
|
// console.log(error);
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// };
|
||||||
|
// export default sendEmail;
|
||||||
|
|
||||||
import { createTransport } from "nodemailer";
|
import { createTransport } from "nodemailer";
|
||||||
|
|
||||||
|
// 1. Fix environment variable names (SMTP not SMPT)
|
||||||
const transporter = createTransport({
|
const transporter = createTransport({
|
||||||
host: process.env.SMPT_HOST,
|
host: process.env.SMTP_HOST, // Corrected from SMPT_HOST
|
||||||
port: process.env.SMPT_PORT,
|
port: process.env.SMTP_PORT, // Corrected from SMPT_PORT
|
||||||
// service: process.env.SMPT_SERVICE,
|
secure: false, // Required for Brevo's STARTTLS
|
||||||
auth: {
|
auth: {
|
||||||
user: process.env.SMPT_MAIL,
|
user: process.env.SMTP_MAIL || "78ab42003@smtp-brevo.com",
|
||||||
pass: process.env.SMPT_PASSWORD,
|
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) => {
|
const sendEmail = async (options) => {
|
||||||
await transporter.sendMail(options, function (error, info) {
|
try {
|
||||||
if (error) {
|
// Add default from address
|
||||||
console.log(error);
|
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;
|
export default sendEmail;
|
||||||
|
|
||||||
// import sgMail from '@sendgrid/mail';
|
// import sgMail from '@sendgrid/mail';
|
||||||
|
Loading…
Reference in New Issue
Block a user