47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
const nodemailer = require('nodemailer');
|
|
require('dotenv').config();
|
|
|
|
const transporter = nodemailer.createTransport({
|
|
host: process.env.SMTP_HOST || 'smtp.office365.com',
|
|
port: parseInt(process.env.SMTP_PORT || '587', 10),
|
|
secure: process.env.SMTP_SECURE === 'true', // true for 465, false for other ports (587 uses STARTTLS)
|
|
auth: {
|
|
user: process.env.SMTP_USER,
|
|
pass: process.env.SMTP_PASS,
|
|
},
|
|
tls: {
|
|
ciphers: 'SSLv3', // sometimes required for Office365
|
|
rejectUnauthorized: false
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Sends an email using the configured SMTP server.
|
|
* @param {string} to - Recipient email address
|
|
* @param {string} subject - Email subject
|
|
* @param {string} text - Plain text body
|
|
* @param {string} html - HTML body (optional)
|
|
* @returns {Promise<any>} Info object from nodemailer
|
|
*/
|
|
const sendMail = async (to, subject, text, html = '') => {
|
|
try {
|
|
const info = await transporter.sendMail({
|
|
from: process.env.SMTP_FROM,
|
|
to,
|
|
subject,
|
|
text,
|
|
html
|
|
});
|
|
console.log(`Email sent to ${to}: ${info.messageId}`);
|
|
return info;
|
|
} catch (error) {
|
|
console.error('Error sending email:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
sendMail,
|
|
transporter
|
|
};
|