Files
otrs-turbo/utils/smtpMailer.js
T

63 lines
1.6 KiB
JavaScript

/**
* utils/smtpMailer.js
* Fallback SMTP per l'invio email via nodemailer.
* Usato se AZURE_TENANT_ID non è configurato ma SMTP_HOST lo è.
*/
'use strict';
const nodemailer = require('nodemailer');
let _transporter = null;
function getTransporter() {
if (_transporter) return _transporter;
_transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: parseInt(process.env.SMTP_PORT || '587', 10),
secure: process.env.SMTP_SECURE === 'true',
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASSWORD,
},
});
return _transporter;
}
/**
* Invia una email tramite SMTP (nodemailer).
* Stessa interfaccia di graphMailer.sendMail.
*/
async function sendMail({ to, cc = [], bcc = [], subject, bodyHtml, attachments = [], inlineImages = [], inReplyTo, references }) {
const transporter = getTransporter();
const mailOptions = {
from: process.env.SMTP_FROM || process.env.SMTP_USER,
to: to.join(', '),
cc: cc.length ? cc.join(', ') : undefined,
bcc: bcc.length ? bcc.join(', ') : undefined,
subject,
html: bodyHtml,
inReplyTo,
references,
attachments: [
...attachments.map(a => ({
filename: a.filename,
content: Buffer.from(a.content, 'base64'),
contentType: a.contentType || 'application/octet-stream',
})),
...inlineImages.map(img => ({
filename: img.cid,
cid: img.cid,
content: Buffer.from(img.content, 'base64'),
contentType: img.contentType || 'image/png',
})),
],
};
await transporter.sendMail(mailOptions);
}
module.exports = { sendMail };