Update Utils/sendEmail.js

This commit is contained in:
gitadmin 2025-04-28 15:52:56 +00:00
parent fb699991fc
commit 00ddd41758

View File

@ -19,45 +19,39 @@
// });
// };
// 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
host: process.env.SMTP_HOST || "smtp-relay.brevo.com", // Fixed typo SMPT->SMTP
port: process.env.SMTP_PORT || 587, // Fixed typo
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
ciphers: 'SSLv3'
}
});
// 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
const sendEmail = async (options) => {
return new Promise((resolve, reject) => {
transporter.sendMail({
...options,
from: options.from || `Cheminova <${process.env.SEND_EMAIL_FROM || "cheminova2004@gmail.com"}>`
}, (error, info) => {
if (error) {
console.log('Email send error:', error);
reject(error);
} else {
resolve(info);
}
});
});
};
export default sendEmail;
// import sgMail from '@sendgrid/mail';
// sgMail.setApiKey(process.env.SENDGRID_API_KEY)
// // console.log(process.env.SENDGRID_API_KEY)