Last active
November 14, 2024 00:51
-
-
Save leodevbro/f8f1ac95a50ff733976484e85ab96c3d to your computer and use it in GitHub Desktop.
This is a simplified version of accessing some values (sender, receiver, cc, bcc, attachments size...) of mails inside mbox file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nodeMbox from 'node-mbox'; | |
import { simpleParser } from 'mailparser'; | |
import { createReadStream } from 'node:fs'; | |
const mboxFilePath = './All mail Including Spam and Trash.mbox'; | |
const scanHeaders = (headersMap) => { | |
const headers = headersMap; | |
const init_From = headers.get('from'); | |
const init_To = headers.get('to'); | |
const init_cc = headers.get('cc'); | |
const init_bcc = headers.get('bcc'); | |
// | |
const init_date = headers.get('date'); | |
const init_messageId = headers.get('message-id'); | |
const initialMainInfoForThisMail = { | |
from: init_From, | |
to: init_To, | |
cc: init_cc, | |
bcc: init_bcc, | |
// | |
date: init_date, | |
['message-id']: init_messageId, | |
}; | |
return initialMainInfoForThisMail; | |
}; | |
const processParsedMail = (theParsedMail) => { | |
const initialMainInfoForThisMail = scanHeaders(theParsedMail.headers); | |
// bytes | |
const sumOfSizesOfAttachmentsOfOneMail = theParsedMail.attachments.reduce( | |
(accu, curr) => accu + curr.size, | |
0, | |
); | |
const countOfAttachmentsInThisMail = theParsedMail.attachments.length; | |
// do something with: | |
// initialMainInfoForThisMail | |
// sumOfSizesOfAttachmentsOfOneMail | |
// countOfAttachmentsInThisMail | |
}; | |
const processOneMail = async (rawMailBuffer) => { | |
const parsedMail = await simpleParser(rawMailBuffer, {}); | |
processParsedMail(parsedMail); | |
}; | |
const analyzeMbox = () => { | |
const mailbox = createReadStream(mboxFilePath); | |
const mbox = nodeMbox.MboxStream(mailbox, { | |
/* options */ | |
}); | |
mbox.on('data', function (msg) { | |
try { | |
processOneMail(msg); | |
} catch (err) { | |
console.log(err); | |
} | |
}); | |
mbox.on('error', function (err) { | |
console.log('got an error', err); | |
}); | |
mbox.on('finish', async function () { | |
console.log( | |
`Finished accessing all the mails, but not yet finished proccessing them. Please wait.`, | |
); | |
// Wait for all proccessings to finish, and then do something | |
}); | |
}; | |
analyzeMbox(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment