Update Utils/sendEmail.js
This commit is contained in:
parent
40eeb098df
commit
e84897d243
@ -1,23 +1,77 @@
|
|||||||
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";
|
||||||
|
|
||||||
const transporter = createTransport({
|
const transporter = createTransport({
|
||||||
host: process.env.SMPT_HOST,
|
host: process.env.SMTP_HOST || "smtp-relay.brevo.com",
|
||||||
port: process.env.SMPT_PORT,
|
port: process.env.SMTP_PORT || 587,
|
||||||
// service: process.env.SMPT_SERVICE,
|
secure: false, // 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',
|
||||||
|
rejectUnauthorized: true
|
||||||
|
},
|
||||||
|
logger: process.env.NODE_ENV === 'development'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
const sendEmail = async (options) => {
|
const sendEmail = async (options) => {
|
||||||
await transporter.sendMail(options, function (error, info) {
|
const mailOptions = {
|
||||||
if (error) {
|
from: options.from || `Cheminova <${process.env.SEND_EMAIL_FROM || "cheminova2004@gmail.com"}>`,
|
||||||
console.log(error);
|
to: options.to,
|
||||||
}
|
subject: options.subject,
|
||||||
});
|
html: options.html,
|
||||||
|
text: options.text || convertHtmlToText(options.html), // Auto plaintext fallback
|
||||||
|
attachments: options.attachments || []
|
||||||
};
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const info = await transporter.sendMail(mailOptions);
|
||||||
|
console.log(`Email sent to ${mailOptions.to} (${options.subject})`);
|
||||||
|
return info;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Email failed:', {
|
||||||
|
to: mailOptions.to,
|
||||||
|
error: error.message
|
||||||
|
});
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Helper for plaintext fallback
|
||||||
|
function convertHtmlToText(html) {
|
||||||
|
return html
|
||||||
|
? html.replace(/<[^>]*>/g, '').replace(/\n{3,}/g, '\n\n')
|
||||||
|
: '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify connection on startup
|
||||||
|
transporter.verify()
|
||||||
|
.then(() => console.log('SMTP connection established'))
|
||||||
|
.catch(err => console.error('SMTP connection failed:', err));
|
||||||
|
|
||||||
export default sendEmail;
|
export default sendEmail;
|
||||||
|
|
||||||
// import sgMail from '@sendgrid/mail';
|
// import sgMail from '@sendgrid/mail';
|
||||||
|
Loading…
Reference in New Issue
Block a user