feat: aggiunta ricerca spudorata. progress: ripreso con il tentativo di invio delle mail che non creino nuovi ticket

This commit is contained in:
VVER440 committed 2026-08-25 23:24:48 +02:00
1 parent 3650bf5790
commit 8d24759a15
18 files changed
+1661 -98

No files matched your search

+43 -16
View File
@@ -47,6 +47,19 @@ async function getAccessToken() {
return cachedToken;
}
function parseSender(raw) {
if (!raw) return { name: '', address: '' };
const str = raw.trim();
const match = str.match(/^(?:"?([^"]*)"?\s+)?<?([^\s@]+@[^\s@>]+)>?$/);
if (match) {
return {
name: (match[1] || '').trim(),
address: match[2].trim(),
};
}
return { name: '', address: str };
}
/**
* Invia una email tramite Microsoft Graph API.
*
@@ -61,8 +74,11 @@ async function getAccessToken() {
* @returns {Promise<void>}
*/
async function sendMail({ to, cc = [], bcc = [], subject, bodyHtml, attachments = [], inlineImages = [], inReplyTo, references, messageId }) {
const sender = process.env.AZURE_MAIL_SENDER;
if (!sender) throw new Error('AZURE_MAIL_SENDER non configurato nel .env');
const rawSender = process.env.AZURE_MAIL_SENDER;
if (!rawSender) throw new Error('AZURE_MAIL_SENDER non configurato nel .env');
const { name: senderName, address: senderAddress } = parseSender(rawSender);
if (!senderAddress) throw new Error('Indirizzo email in AZURE_MAIL_SENDER non valido');
const token = await getAccessToken();
@@ -102,23 +118,34 @@ async function sendMail({ to, cc = [], bcc = [], subject, bodyHtml, attachments
headers.push({ name: 'References', value: references });
}
const payload = {
message: {
subject,
body: {
contentType: 'HTML',
content: bodyHtml,
},
toRecipients,
ccRecipients,
bccRecipients,
attachments: allAttachments,
internetMessageHeaders: headers.length ? headers : undefined,
const messageObj = {
subject,
body: {
contentType: 'HTML',
content: bodyHtml,
},
toRecipients,
ccRecipients,
bccRecipients,
attachments: allAttachments,
internetMessageHeaders: headers.length ? headers : undefined,
};
if (senderName) {
messageObj.from = {
emailAddress: {
name: senderName,
address: senderAddress,
}
};
}
const payload = {
message: messageObj,
saveToSentItems: true,
};
const url = `https://graph.microsoft.com/v1.0/users/${encodeURIComponent(sender)}/sendMail`;
const url = `https://graph.microsoft.com/v1.0/users/${encodeURIComponent(senderAddress)}/sendMail`;
const res = await fetch(url, {
method: 'POST',
@@ -135,7 +162,7 @@ async function sendMail({ to, cc = [], bcc = [], subject, bodyHtml, attachments
// Wait 1.5 seconds for Exchange to process and place it in Sent Items
await new Promise(resolve => setTimeout(resolve, 1500));
const searchUrl = `https://graph.microsoft.com/v1.0/users/${encodeURIComponent(sender)}/mailFolders/sentItems/messages?$filter=subject eq '${subject.replace(/'/g, "''")}'&$top=1&$select=internetMessageId`;
const searchUrl = `https://graph.microsoft.com/v1.0/users/${encodeURIComponent(senderAddress)}/mailFolders/sentItems/messages?$filter=subject eq '${subject.replace(/'/g, "''")}'&$top=1&$select=internetMessageId`;
const searchRes = await fetch(searchUrl, {
headers: {
'Authorization': `Bearer ${token}`,
+2 -2
View File
@@ -29,11 +29,11 @@ function getTransporter() {
* Invia una email tramite SMTP (nodemailer).
* Stessa interfaccia di graphMailer.sendMail.
*/
async function sendMail({ to, cc = [], bcc = [], subject, bodyHtml, attachments = [], inlineImages = [], inReplyTo, references, messageId }) {
async function sendMail({ from, to, cc = [], bcc = [], subject, bodyHtml, attachments = [], inlineImages = [], inReplyTo, references, messageId }) {
const transporter = getTransporter();
const mailOptions = {
from: process.env.SMTP_FROM || process.env.SMTP_USER,
from: from || process.env.SMTP_FROM || process.env.SMTP_USER,
to: to.join(', '),
cc: cc.length ? cc.join(', ') : undefined,
bcc: bcc.length ? bcc.join(', ') : undefined,