Update Utils/sendEmail.js
This commit is contained in:
parent
fb699991fc
commit
00ddd41758
@ -19,45 +19,39 @@
|
|||||||
// });
|
// });
|
||||||
// };
|
// };
|
||||||
// export default sendEmail;
|
// 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.SMTP_HOST, // Corrected from SMPT_HOST
|
host: process.env.SMTP_HOST || "smtp-relay.brevo.com", // Fixed typo SMPT->SMTP
|
||||||
port: process.env.SMTP_PORT, // Corrected from SMPT_PORT
|
port: process.env.SMTP_PORT || 587, // Fixed typo
|
||||||
secure: false, // Required for Brevo's STARTTLS
|
secure: false, // Required for Brevo's STARTTLS
|
||||||
auth: {
|
auth: {
|
||||||
user: process.env.SMTP_MAIL || "78ab42003@smtp-brevo.com",
|
user: process.env.SMTP_MAIL || "78ab42003@smtp-brevo.com",
|
||||||
pass: process.env.SMTP_PASSWORD || "saTOdNcySftx2PXG"
|
pass: process.env.SMTP_PASSWORD || "saTOdNcySftx2PXG"
|
||||||
},
|
},
|
||||||
tls: {
|
tls: {
|
||||||
ciphers: 'SSLv3', // Match Python's encryption
|
ciphers: 'SSLv3'
|
||||||
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);
|
const sendEmail = async (options) => {
|
||||||
console.log('Email sent to %s', mailOptions.to);
|
return new Promise((resolve, reject) => {
|
||||||
return info;
|
transporter.sendMail({
|
||||||
} catch (error) {
|
...options,
|
||||||
console.error('Email send failed:', error);
|
from: options.from || `Cheminova <${process.env.SEND_EMAIL_FROM || "cheminova2004@gmail.com"}>`
|
||||||
throw new Error(`EMAIL_FAILED: ${error.message}`); // Propagate specific error
|
}, (error, info) => {
|
||||||
|
if (error) {
|
||||||
|
console.log('Email send error:', error);
|
||||||
|
reject(error);
|
||||||
|
} else {
|
||||||
|
resolve(info);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export default sendEmail;
|
export default sendEmail;
|
||||||
|
|
||||||
// import sgMail from '@sendgrid/mail';
|
// import sgMail from '@sendgrid/mail';
|
||||||
// sgMail.setApiKey(process.env.SENDGRID_API_KEY)
|
// sgMail.setApiKey(process.env.SENDGRID_API_KEY)
|
||||||
// // console.log(process.env.SENDGRID_API_KEY)
|
// // console.log(process.env.SENDGRID_API_KEY)
|
||||||
|
Loading…
Reference in New Issue
Block a user