Refactor getMailBody function to work also with annidated multiparts. see #335
This commit is contained in:
parent
904550f6e6
commit
6494a4f48f
2 changed files with 30 additions and 30 deletions
|
|
@ -108,41 +108,41 @@ export async function getMailSubject(tab){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getMailBody(fullMessage){
|
function extractTextParts(fullMessage) {
|
||||||
let text = '';
|
const textParts = [];
|
||||||
let html = '';
|
|
||||||
|
|
||||||
// console.log(">>>>>>>>>> fullMessage.contentType.trim().toLowerCase(): " + fullMessage.contentType.trim().toLowerCase());
|
function walkParts(parts) {
|
||||||
// console.log(">>>>>>>>>> fullMessage.body: " + fullMessage.body);
|
for (const part of parts) {
|
||||||
|
if (part.parts && part.parts.length > 0) {
|
||||||
if (fullMessage.contentType.trim().toLowerCase() === "text/plain") {
|
// Recursively walk through sub-parts
|
||||||
text = fullMessage.body;
|
walkParts(part.parts);
|
||||||
}
|
} else {
|
||||||
if (fullMessage.contentType.trim().toLowerCase() === "text/html") {
|
// Check if contentType starts with "text/"
|
||||||
html = fullMessage.body;
|
if (part.contentType && part.contentType.startsWith("text/")) {
|
||||||
}
|
textParts.push(part);
|
||||||
|
|
||||||
if((text == undefined || text == null || text == '') && (html == undefined || html == null || html == '')) {
|
|
||||||
for (let part of fullMessage.parts) {
|
|
||||||
if (part.contentType.trim().toLowerCase() === "text/plain") {
|
|
||||||
text = part.body;
|
|
||||||
}
|
|
||||||
if (part.contentType.trim().toLowerCase() === "text/html") {
|
|
||||||
html = part.body;
|
|
||||||
}
|
|
||||||
if((text == undefined || text == null || text == '') && (html == undefined || html == null || html == '')) {
|
|
||||||
for (let subpart of part.parts) {
|
|
||||||
if (subpart.contentType.trim().toLowerCase() === "text/plain") {
|
|
||||||
text = subpart.body;
|
|
||||||
}
|
|
||||||
if (subpart.contentType.trim().toLowerCase() === "text/html") {
|
|
||||||
html = subpart.body;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (fullMessage.parts && fullMessage.parts.length > 0) {
|
||||||
|
walkParts(fullMessage.parts);
|
||||||
|
}
|
||||||
|
|
||||||
|
return textParts;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getMailBody(fullMessage){
|
||||||
|
const textParts = extractTextParts(fullMessage);
|
||||||
|
let text = "";
|
||||||
|
let html = "";
|
||||||
|
for (const part of textParts) {
|
||||||
|
if (part.contentType === "text/plain") {
|
||||||
|
text += part.body;
|
||||||
|
} else if (part.contentType === "text/html") {
|
||||||
|
html += part.body;
|
||||||
|
}
|
||||||
|
}
|
||||||
return {text, html};
|
return {text, html};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -808,7 +808,7 @@ async function processEmails(messages, addTagsAuto, spamFilter) {
|
||||||
|
|
||||||
if (addTagsAuto || spamFilter) {
|
if (addTagsAuto || spamFilter) {
|
||||||
curr_fullMessage = await browser.messages.getFull(message.id);
|
curr_fullMessage = await browser.messages.getFull(message.id);
|
||||||
msg_text = await getMailBody(curr_fullMessage);
|
msg_text = getMailBody(curr_fullMessage);
|
||||||
body_text = msg_text.text.replace(/\s+/g, ' ').trim();
|
body_text = msg_text.text.replace(/\s+/g, ' ').trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue