mail page fix
This commit is contained in:
parent
ca090f1ebb
commit
0cfc45f13a
@ -323,13 +323,18 @@ function getReplyBody(email: Email | null, type: 'reply' | 'replyAll' | 'forward
|
|||||||
try {
|
try {
|
||||||
// Split email into headers and body
|
// Split email into headers and body
|
||||||
const [headersPart, ...bodyParts] = email.body.split('\r\n\r\n');
|
const [headersPart, ...bodyParts] = email.body.split('\r\n\r\n');
|
||||||
|
if (!headersPart || bodyParts.length === 0) {
|
||||||
|
throw new Error('Invalid email format: missing headers or body');
|
||||||
|
}
|
||||||
|
|
||||||
const body = bodyParts.join('\r\n\r\n');
|
const body = bodyParts.join('\r\n\r\n');
|
||||||
|
|
||||||
// Parse headers using our MIME decoder
|
// Parse headers using Infomaniak MIME decoder
|
||||||
const headerInfo = parseEmailHeaders(headersPart);
|
const headerInfo = parseEmailHeaders(headersPart);
|
||||||
const boundary = extractBoundary(headersPart);
|
const boundary = extractBoundary(headersPart);
|
||||||
|
|
||||||
let content = '';
|
let content = '';
|
||||||
|
let isHtml = false;
|
||||||
|
|
||||||
// If it's a multipart email
|
// If it's a multipart email
|
||||||
if (boundary) {
|
if (boundary) {
|
||||||
@ -339,23 +344,76 @@ function getReplyBody(email: Email | null, type: 'reply' | 'replyAll' | 'forward
|
|||||||
if (!part.trim()) continue;
|
if (!part.trim()) continue;
|
||||||
|
|
||||||
const [partHeaders, ...partBodyParts] = part.split('\r\n\r\n');
|
const [partHeaders, ...partBodyParts] = part.split('\r\n\r\n');
|
||||||
|
if (!partHeaders || partBodyParts.length === 0) continue;
|
||||||
|
|
||||||
const partBody = partBodyParts.join('\r\n\r\n');
|
const partBody = partBodyParts.join('\r\n\r\n');
|
||||||
const partHeaderInfo = parseEmailHeaders(partHeaders);
|
const contentType = extractHeader(partHeaders, 'Content-Type').toLowerCase();
|
||||||
|
const encoding = extractHeader(partHeaders, 'Content-Transfer-Encoding').toLowerCase();
|
||||||
|
const charset = extractHeader(partHeaders, 'charset') || 'utf-8';
|
||||||
|
|
||||||
if (partHeaderInfo.contentType.includes('text/plain')) {
|
try {
|
||||||
content = decodeQuotedPrintable(partBody, partHeaderInfo.charset);
|
let decodedContent = '';
|
||||||
break;
|
if (encoding === 'base64') {
|
||||||
} else if (partHeaderInfo.contentType.includes('text/html') && !content) {
|
decodedContent = decodeBase64(partBody, charset);
|
||||||
content = cleanHtml(decodeQuotedPrintable(partBody, partHeaderInfo.charset));
|
} else if (encoding === 'quoted-printable') {
|
||||||
|
decodedContent = decodeQuotedPrintable(partBody, charset);
|
||||||
|
} else {
|
||||||
|
decodedContent = convertCharset(partBody, charset);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contentType.includes('text/html')) {
|
||||||
|
// For HTML content, preserve the HTML structure
|
||||||
|
content = decodedContent
|
||||||
|
.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '')
|
||||||
|
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')
|
||||||
|
.replace(/<meta[^>]*>/gi, '')
|
||||||
|
.replace(/<link[^>]*>/gi, '')
|
||||||
|
.replace(/<base[^>]*>/gi, '')
|
||||||
|
.replace(/<title[^>]*>[\s\S]*?<\/title>/gi, '')
|
||||||
|
.replace(/<head[^>]*>[\s\S]*?<\/head>/gi, '')
|
||||||
|
.replace(/<body[^>]*>/gi, '')
|
||||||
|
.replace(/<\/body>/gi, '')
|
||||||
|
.replace(/<html[^>]*>/gi, '')
|
||||||
|
.replace(/<\/html>/gi, '');
|
||||||
|
isHtml = true;
|
||||||
|
break; // Prefer HTML content
|
||||||
|
} else if (contentType.includes('text/plain') && !content) {
|
||||||
|
content = decodedContent;
|
||||||
|
}
|
||||||
|
} catch (partError) {
|
||||||
|
console.error('Error processing email part:', partError);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no content found or not multipart, try to decode the whole body
|
// If no content found or not multipart, try to decode the whole body
|
||||||
if (!content) {
|
if (!content) {
|
||||||
content = decodeQuotedPrintable(body, headerInfo.charset);
|
const encoding = extractHeader(headersPart, 'Content-Transfer-Encoding').toLowerCase();
|
||||||
|
const charset = extractHeader(headersPart, 'charset') || 'utf-8';
|
||||||
|
|
||||||
|
if (encoding === 'base64') {
|
||||||
|
content = decodeBase64(body, charset);
|
||||||
|
} else if (encoding === 'quoted-printable') {
|
||||||
|
content = decodeQuotedPrintable(body, charset);
|
||||||
|
} else {
|
||||||
|
content = convertCharset(body, charset);
|
||||||
|
}
|
||||||
|
|
||||||
if (headerInfo.contentType.includes('text/html')) {
|
if (headerInfo.contentType.includes('text/html')) {
|
||||||
content = cleanHtml(content);
|
content = content
|
||||||
|
.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '')
|
||||||
|
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')
|
||||||
|
.replace(/<meta[^>]*>/gi, '')
|
||||||
|
.replace(/<link[^>]*>/gi, '')
|
||||||
|
.replace(/<base[^>]*>/gi, '')
|
||||||
|
.replace(/<title[^>]*>[\s\S]*?<\/title>/gi, '')
|
||||||
|
.replace(/<head[^>]*>[\s\S]*?<\/head>/gi, '')
|
||||||
|
.replace(/<body[^>]*>/gi, '')
|
||||||
|
.replace(/<\/body>/gi, '')
|
||||||
|
.replace(/<html[^>]*>/gi, '')
|
||||||
|
.replace(/<\/html>/gi, '');
|
||||||
|
isHtml = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -370,31 +428,46 @@ function getReplyBody(email: Email | null, type: 'reply' | 'replyAll' | 'forward
|
|||||||
hour12: false
|
hour12: false
|
||||||
});
|
});
|
||||||
|
|
||||||
let replyHeader = '';
|
const from = email.fromName || email.from;
|
||||||
|
const subject = email.subject || '(No subject)';
|
||||||
|
|
||||||
if (type === 'forward') {
|
if (type === 'forward') {
|
||||||
replyHeader = `\n\n---------- Forwarded message ----------\n`;
|
if (isHtml) {
|
||||||
replyHeader += `From: ${email.from}\n`;
|
return `---------- Forwarded message ---------<br>
|
||||||
replyHeader += `Date: ${formattedDate}\n`;
|
From: ${from}<br>
|
||||||
replyHeader += `Subject: ${email.subject}\n`;
|
Date: ${formattedDate}<br>
|
||||||
replyHeader += `To: ${email.to}\n`;
|
Subject: ${subject}<br>
|
||||||
if (email.cc) {
|
To: ${email.to}<br>
|
||||||
replyHeader += `Cc: ${email.cc}\n`;
|
<br>
|
||||||
|
${content}`;
|
||||||
|
} else {
|
||||||
|
return `---------- Forwarded message ---------
|
||||||
|
From: ${from}
|
||||||
|
Date: ${formattedDate}
|
||||||
|
Subject: ${subject}
|
||||||
|
To: ${email.to}
|
||||||
|
|
||||||
|
${content}`;
|
||||||
}
|
}
|
||||||
replyHeader += `\n`;
|
|
||||||
} else {
|
} else {
|
||||||
replyHeader = `\n\nOn ${formattedDate}, ${email.from} wrote:\n`;
|
if (isHtml) {
|
||||||
|
return `<br><br>
|
||||||
|
<div class="email-quote">
|
||||||
|
<div class="email-quote-header">
|
||||||
|
On ${formattedDate}, ${from} wrote:
|
||||||
|
</div>
|
||||||
|
<div class="email-quote-content">
|
||||||
|
${content}
|
||||||
|
</div>
|
||||||
|
</div>`;
|
||||||
|
} else {
|
||||||
|
return `\n\nOn ${formattedDate}, ${from} wrote:
|
||||||
|
> ${content.split('\n').join('\n> ')}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add reply prefix to each line
|
|
||||||
const prefixedContent = content
|
|
||||||
.split('\n')
|
|
||||||
.map(line => `> ${line}`)
|
|
||||||
.join('\n');
|
|
||||||
|
|
||||||
return replyHeader + prefixedContent;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error getting reply body:', error);
|
console.error('Error formatting reply:', error);
|
||||||
return email.body;
|
return '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user