Update Utils/sendEmail.js
This commit is contained in:
parent
e42ec58002
commit
c1b46cb720
@ -19,61 +19,40 @@
|
|||||||
// });
|
// });
|
||||||
// };
|
// };
|
||||||
// export default sendEmail;
|
// export default sendEmail;
|
||||||
import { createTransport } from "nodemailer";
|
import nodemailer from 'nodemailer';
|
||||||
|
|
||||||
const transporter = createTransport({
|
const transporter = nodemailer.createTransport({
|
||||||
host: process.env.SMTP_HOST || "smtp-relay.brevo.com",
|
host: "smtp-relay.brevo.com",
|
||||||
port: process.env.SMTP_PORT || 587,
|
port: 587,
|
||||||
secure: false, // STARTTLS
|
secure: false,
|
||||||
auth: {
|
auth: {
|
||||||
user: process.env.SMTP_MAIL || "78ab42003@smtp-brevo.com",
|
user: "78ab42003@smtp-brevo.com",
|
||||||
pass: process.env.SMTP_PASSWORD || "saTOdNcySftx2PXG"
|
pass: "saTOdNcySftx2PXG"
|
||||||
},
|
},
|
||||||
tls: {
|
tls: {
|
||||||
ciphers: 'SSLv3',
|
rejectUnauthorized: true // Enable for production
|
||||||
rejectUnauthorized: true
|
}
|
||||||
},
|
|
||||||
logger: process.env.NODE_ENV === 'development'
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export default async function sendEmail(options) {
|
||||||
const sendEmail = async (options) => {
|
try {
|
||||||
const mailOptions = {
|
const info = await transporter.sendMail({
|
||||||
from: options.from || `Cheminova <${process.env.SEND_EMAIL_FROM || "cheminova2004@gmail.com"}>`,
|
from: options.from || `Cheminova <cheminova2004@gmail.com>`,
|
||||||
to: options.to,
|
to: options.to,
|
||||||
subject: options.subject,
|
subject: options.subject,
|
||||||
html: options.html,
|
html: options.html,
|
||||||
text: options.text || convertHtmlToText(options.html), // Auto plaintext fallback
|
text: options.text || options.html.replace(/<[^>]*>/g, '') // Auto plaintext
|
||||||
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;
|
|
||||||
|
console.log('Email sent to', options.to);
|
||||||
|
return { success: true, messageId: info.messageId };
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Email failed:', error.message);
|
||||||
|
return { success: false, error: error.message };
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
|
|
||||||
// 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