From 3dd9193c20f3aa1e01228736858643aa3c3088aa Mon Sep 17 00:00:00 2001 From: alma Date: Sun, 18 Jan 2026 14:50:33 +0100 Subject: [PATCH] SignIn --- lib/services/microsoft-graph-mail.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/services/microsoft-graph-mail.ts b/lib/services/microsoft-graph-mail.ts index b87800e..fdfa4bc 100644 --- a/lib/services/microsoft-graph-mail.ts +++ b/lib/services/microsoft-graph-mail.ts @@ -194,10 +194,12 @@ export async function fetchGraphEmail( try { const client = await getMicrosoftGraphClient(mailCredentialId); - // First, get the message without attachments to check if it has attachments + // First, try to get the message with attachments included + // Microsoft Graph API may include attachments in the initial response if we request them const response = await client.get(`/me/messages/${messageId}`, { params: { - '$select': 'id,subject,from,toRecipients,ccRecipients,bccRecipients,body,bodyPreview,receivedDateTime,sentDateTime,isRead,hasAttachments,importance,flag', + '$expand': 'attachments', + '$select': 'id,subject,from,toRecipients,ccRecipients,bccRecipients,body,bodyPreview,receivedDateTime,sentDateTime,isRead,hasAttachments,importance,flag,attachments', }, }); @@ -206,24 +208,22 @@ export async function fetchGraphEmail( logger.debug('Fetched email from Graph API', { messageId, hasAttachments: message.hasAttachments, + attachmentsInResponse: !!message.attachments, + attachmentsCount: message.attachments?.length || 0, mailCredentialIdHash: Buffer.from(mailCredentialId).toString('base64').slice(0, 12), }); - // If email has attachments, fetch them separately - // Microsoft Graph API sometimes doesn't include attachments in the initial response - if (message.hasAttachments) { + // If email has attachments but they weren't included in the response, fetch them separately + if (message.hasAttachments && (!message.attachments || message.attachments.length === 0)) { try { logger.debug('Fetching attachments list from Graph API', { messageId, mailCredentialIdHash: Buffer.from(mailCredentialId).toString('base64').slice(0, 12), }); - // Fetch attachments separately - const attachmentsResponse = await client.get(`/me/messages/${messageId}/attachments`, { - params: { - '$select': 'id,name,contentType,size,contentBytes,isInline', - }, - }); + // Fetch attachments separately - don't use $select as it may cause issues + // Microsoft Graph API will return all attachment properties by default + const attachmentsResponse = await client.get(`/me/messages/${messageId}/attachments`); const attachments = attachmentsResponse.data.value || []; @@ -279,6 +279,9 @@ export async function fetchGraphEmail( messageId, error: attachmentsError instanceof Error ? attachmentsError.message : String(attachmentsError), status: attachmentsError.response?.status, + statusText: attachmentsError.response?.statusText, + responseData: attachmentsError.response?.data, + url: `/me/messages/${messageId}/attachments`, }); // Continue without attachments if fetch fails message.attachments = [];