courrier msft oauth
This commit is contained in:
parent
92e0c0dd3b
commit
92b439dabf
@ -846,6 +846,18 @@ export async function getEmails(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Map email addresses safely with null checks
|
||||||
|
function mapAddresses(addresses: any[] | undefined): Array<{ name: string; address: string }> {
|
||||||
|
if (!addresses || !Array.isArray(addresses)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return addresses.map((addr: any) => ({
|
||||||
|
name: addr.name || addr.address || '',
|
||||||
|
address: addr.address || ''
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a single email with full content
|
* Get a single email with full content
|
||||||
*/
|
*/
|
||||||
@ -894,6 +906,8 @@ export async function getEmailContent(
|
|||||||
const client = await getImapConnection(userId, effectiveAccountId);
|
const client = await getImapConnection(userId, effectiveAccountId);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
await client.mailboxOpen(normalizedFolder);
|
||||||
|
|
||||||
// Log connection details with account context
|
// Log connection details with account context
|
||||||
console.log(`[DEBUG] Fetching email ${emailId} from folder ${normalizedFolder} for account ${effectiveAccountId}`);
|
console.log(`[DEBUG] Fetching email ${emailId} from folder ${normalizedFolder} for account ${effectiveAccountId}`);
|
||||||
|
|
||||||
@ -926,25 +940,47 @@ export async function getEmailContent(
|
|||||||
const sequenceNumber = searchResult[0];
|
const sequenceNumber = searchResult[0];
|
||||||
console.log(`[DEBUG] Found sequence number ${sequenceNumber} for UID ${numericId} in account ${effectiveAccountId}`);
|
console.log(`[DEBUG] Found sequence number ${sequenceNumber} for UID ${numericId} in account ${effectiveAccountId}`);
|
||||||
|
|
||||||
// Now fetch using the sequence number
|
// Now fetch using the sequence number with error handling
|
||||||
const message = await client.fetchOne(sequenceNumber.toString(), {
|
let message;
|
||||||
source: true,
|
try {
|
||||||
envelope: true,
|
message = await client.fetchOne(sequenceNumber.toString(), {
|
||||||
flags: true,
|
source: true,
|
||||||
size: true
|
envelope: true,
|
||||||
});
|
flags: true,
|
||||||
|
size: true
|
||||||
|
});
|
||||||
|
} catch (fetchError) {
|
||||||
|
console.error(`Error fetching message with sequence ${sequenceNumber}:`, fetchError);
|
||||||
|
throw new Error(`Failed to fetch email: ${fetchError instanceof Error ? fetchError.message : 'Unknown error'}`);
|
||||||
|
}
|
||||||
|
|
||||||
if (!message) {
|
if (!message) {
|
||||||
throw new Error(`Email not found with sequence number ${sequenceNumber} in folder ${normalizedFolder} for account ${effectiveAccountId}`);
|
throw new Error(`Email not found with sequence number ${sequenceNumber} in folder ${normalizedFolder} for account ${effectiveAccountId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if message has required fields
|
||||||
|
if (!message.source || !message.envelope) {
|
||||||
|
throw new Error(`Invalid email data received: missing source or envelope data`);
|
||||||
|
}
|
||||||
|
|
||||||
const { source, envelope, flags, size } = message;
|
const { source, envelope, flags, size } = message;
|
||||||
|
|
||||||
|
// Validate envelope data
|
||||||
|
if (!envelope) {
|
||||||
|
throw new Error('Email envelope data is missing');
|
||||||
|
}
|
||||||
|
|
||||||
// Parse the email content, ensuring all styles and structure are preserved
|
// Parse the email content, ensuring all styles and structure are preserved
|
||||||
const parsedEmail = await simpleParser(source.toString(), {
|
let parsedEmail;
|
||||||
skipHtmlToText: true,
|
try {
|
||||||
keepCidLinks: true
|
parsedEmail = await simpleParser(source.toString(), {
|
||||||
});
|
skipHtmlToText: true,
|
||||||
|
keepCidLinks: true
|
||||||
|
});
|
||||||
|
} catch (parseError) {
|
||||||
|
console.error(`Error parsing email content for ${emailId}:`, parseError);
|
||||||
|
throw new Error(`Failed to parse email content: ${parseError instanceof Error ? parseError.message : 'Unknown error'}`);
|
||||||
|
}
|
||||||
|
|
||||||
// Convert flags from Set to boolean checks
|
// Convert flags from Set to boolean checks
|
||||||
const flagsArray = Array.from(flags as Set<string>);
|
const flagsArray = Array.from(flags as Set<string>);
|
||||||
@ -956,22 +992,10 @@ export async function getEmailContent(
|
|||||||
id: emailId,
|
id: emailId,
|
||||||
messageId: envelope.messageId,
|
messageId: envelope.messageId,
|
||||||
subject: envelope.subject || "(No Subject)",
|
subject: envelope.subject || "(No Subject)",
|
||||||
from: envelope.from.map((f: any) => ({
|
from: mapAddresses(envelope.from),
|
||||||
name: f.name || f.address,
|
to: mapAddresses(envelope.to),
|
||||||
address: f.address,
|
cc: mapAddresses(envelope.cc),
|
||||||
})),
|
bcc: mapAddresses(envelope.bcc),
|
||||||
to: envelope.to.map((t: any) => ({
|
|
||||||
name: t.name || t.address,
|
|
||||||
address: t.address,
|
|
||||||
})),
|
|
||||||
cc: (envelope.cc || []).map((c: any) => ({
|
|
||||||
name: c.name || c.address,
|
|
||||||
address: c.address,
|
|
||||||
})),
|
|
||||||
bcc: (envelope.bcc || []).map((b: any) => ({
|
|
||||||
name: b.name || b.address,
|
|
||||||
address: b.address,
|
|
||||||
})),
|
|
||||||
date: envelope.date || new Date(),
|
date: envelope.date || new Date(),
|
||||||
flags: {
|
flags: {
|
||||||
seen: flagsArray.includes("\\Seen"),
|
seen: flagsArray.includes("\\Seen"),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user