UNPKG

eml-parser

Version:

Parse .eml and .msg files or convert to pdf, html, jpeg or png format. Extract headers and attachments from .eml and msg files.

51 lines (43 loc) 2.02 kB
const fs = require('fs'); const path = require('path'); const MsgReader = require('@kenjiuno/msgreader').default; const decompressRTF = require('@kenjiuno/decompressrtf').decompressRTF; const rtfParser = require('rtf-stream-parser'); const iconv = require('iconv-lite'); // Path to a sample .msg file in your workspace const msgFilePath = path.join(__dirname, 'RE_3A__External_FW_3A_AR_DP_IN__2_.msg'); // Read the .msg file as a buffer const msgBuffer = fs.readFileSync(msgFilePath); // Create a MsgReader instance const msgReader = new MsgReader(msgBuffer); // Get the file data (parsed message) const msgData = msgReader.getFileData(); // console.log('Subject:', msgData.subject); // console.log('Sender Name:', msgData.senderName); // console.log('Sender Email:', msgData.senderEmail); // console.log('Recipients:', msgData.recipients); console.log('Body (plain text):', msgData.body); console.log('Body (HTML):', msgData.bodyHtml); if (msgData.html && msgData.html instanceof Uint8Array) { const htmlString = Buffer.from(msgData.html).toString('utf8'); console.log('Decoded HTML body:', htmlString); } else { console.log('Body (html):', msgData.html); } // console.log('Attachments:', msgData.attachments); console.log('All keys in msgData:', Object.keys(msgData)); // console.log('Full msgData object:', JSON.stringify(msgData, null, 2)); fs.writeFileSync('msgData.json', JSON.stringify(msgData, null, 2)); // If RTF body exists, extract and print as plain text if (msgData.compressedRtf) { try { const outputArray = decompressRTF(msgData.compressedRtf); const decompressedRtf = Buffer.from(outputArray).toString('ascii'); const rtfText = rtfParser.deEncapsulateSync(decompressedRtf, { decode: iconv.decode }).text; console.log('RTF Body (plain text):', rtfText); } catch (err) { console.error('Error extracting RTF body:', err); } } else { console.log('No RTF body found in this message.'); }