mail page rest
This commit is contained in:
parent
f4502f15ff
commit
b5ba7ef508
@ -111,14 +111,23 @@ function parseFullEmail(emailContent: string): ParsedEmailContent {
|
|||||||
const headerInfo = parseEmailHeaders(headers);
|
const headerInfo = parseEmailHeaders(headers);
|
||||||
const boundary = extractBoundary(headers);
|
const boundary = extractBoundary(headers);
|
||||||
|
|
||||||
|
// Initialize result object
|
||||||
|
const result: ParsedEmailContent = {
|
||||||
|
headers,
|
||||||
|
body: '',
|
||||||
|
html: undefined,
|
||||||
|
text: undefined,
|
||||||
|
attachments: []
|
||||||
|
};
|
||||||
|
|
||||||
// Handle multipart content
|
// Handle multipart content
|
||||||
if (boundary && headerInfo.contentType.startsWith('multipart/')) {
|
if (boundary && headerInfo.contentType.startsWith('multipart/')) {
|
||||||
const parts = body.split(`--${boundary}`);
|
const parts = body.split(`--${boundary}`);
|
||||||
const processedParts = parts
|
parts
|
||||||
.filter(part => part.trim() && !part.includes('--'))
|
.filter(part => part.trim() && !part.includes('--'))
|
||||||
.map(part => {
|
.forEach(part => {
|
||||||
const partHeaderEnd = part.indexOf('\r\n\r\n');
|
const partHeaderEnd = part.indexOf('\r\n\r\n');
|
||||||
if (partHeaderEnd === -1) return part;
|
if (partHeaderEnd === -1) return;
|
||||||
|
|
||||||
const partHeaders = part.substring(0, partHeaderEnd);
|
const partHeaders = part.substring(0, partHeaderEnd);
|
||||||
const partBody = part.substring(partHeaderEnd + 4);
|
const partBody = part.substring(partHeaderEnd + 4);
|
||||||
@ -131,35 +140,45 @@ function parseFullEmail(emailContent: string): ParsedEmailContent {
|
|||||||
decodedContent = decodeBase64(partBody, partInfo.charset);
|
decodedContent = decodeBase64(partBody, partInfo.charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle different content types
|
||||||
if (partInfo.contentType.includes('text/html')) {
|
if (partInfo.contentType.includes('text/html')) {
|
||||||
decodedContent = cleanHtml(decodedContent);
|
result.html = cleanHtml(decodedContent);
|
||||||
|
} else if (partInfo.contentType.includes('text/plain')) {
|
||||||
|
result.text = decodedContent;
|
||||||
|
} else if (partInfo.contentType.includes('application/') || partInfo.contentType.includes('image/')) {
|
||||||
|
// Handle attachments
|
||||||
|
const filename = extractFilename(partHeaders) || `attachment-${Date.now()}`;
|
||||||
|
result.attachments?.push({
|
||||||
|
filename,
|
||||||
|
content: decodedContent,
|
||||||
|
contentType: partInfo.contentType
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return decodedContent;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
// Set the body to the text content if available, otherwise use HTML
|
||||||
headers,
|
result.body = result.text || result.html || '';
|
||||||
body: processedParts.join('\n\n')
|
} else {
|
||||||
};
|
// Handle single part content
|
||||||
|
let decodedBody = body;
|
||||||
|
if (headerInfo.encoding === 'quoted-printable') {
|
||||||
|
decodedBody = decodeQuotedPrintable(body, headerInfo.charset);
|
||||||
|
} else if (headerInfo.encoding === 'base64') {
|
||||||
|
decodedBody = decodeBase64(body, headerInfo.charset);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (headerInfo.contentType.includes('text/html')) {
|
||||||
|
result.html = cleanHtml(decodedBody);
|
||||||
|
result.body = result.html;
|
||||||
|
} else if (headerInfo.contentType.includes('text/plain')) {
|
||||||
|
result.text = decodedBody;
|
||||||
|
result.body = result.text;
|
||||||
|
} else {
|
||||||
|
result.body = decodedBody;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle single part content
|
return result;
|
||||||
let decodedBody = body;
|
|
||||||
if (headerInfo.encoding === 'quoted-printable') {
|
|
||||||
decodedBody = decodeQuotedPrintable(body, headerInfo.charset);
|
|
||||||
} else if (headerInfo.encoding === 'base64') {
|
|
||||||
decodedBody = decodeBase64(body, headerInfo.charset);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (headerInfo.contentType.includes('text/html')) {
|
|
||||||
decodedBody = cleanHtml(decodedBody);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
headers,
|
|
||||||
body: decodedBody
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractTextFromHtml(html: string): string {
|
function extractTextFromHtml(html: string): string {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user