HiraokaHyperTools / msgreader

35 stars 9 forks source link

Mail nested as attachment isn't retrived by testMsg.getAttachment(attachment) #11

Closed DevPan29 closed 3 years ago

DevPan29 commented 3 years ago

Hi all, I was trying to parse a mail that contain another mail as attachment. Trying to parse the mail attached and getting his content through getAttachment function I get this error:

TypeError: Cannot read property 'name' of undefined
    at getFieldType (C:\SRDEV\workspaces\BFF-DUC-3\duc-bff\node_modules\@kenjiuno\msgreader\lib\MsgReader.js:436:29)
    at MsgReader.getAttachment (C:\SRDEV\workspaces\BFF-DUC-3\duc-bff\node_modules\@kenjiuno\msgreader\lib\MsgReader.js:602:76)

this is the snippet related to the attachments parsing

if (testMsgInfo.attachments && _.isArray(testMsgInfo.attachments)) {
          const attachmentsArray = _.values(testMsgInfo.attachments);
          attachmentsArray.map((attachment, index) => {

              const attachmentMsgReader = testMsg.getAttachment(index);
              attachments.push({
                name: attachment.fileName,
                content: attachmentMsgReader.content as any
              })

          })
        }

This code works with all kinds of attachments but doesn't work with mail as attachment. Trying to debug the library I see that during fieldsDataDocument function, this kind of attachment haven't any fieldClass equal to _ATTACHMENTDATA: '3701' so the dataId isn't properly set.

This library is awesome. Would be great if would exist support for nested mail as attachment

kenjiuno commented 3 years ago

Hmm, in frankly speaking, nested mail is a folder in archive. Thus if we want to extract a nested mail from .msg file: extract specific folder (and subordinates files/folders) from .msg and repack them to new .msg.

.msg archive format is called as Compound File Binary Format - Wikipedia. This format is also used on familiar .doc, .xls, .ppt files.

Currently msgreader will only read Compound file. We need additional method to write down new Compound file.

For visualization purpose 7-Zip can be used, against test/msgInMsg.msg: https://github.com/HiraokaHyperTools/msgreader/blob/master/test/msgInMsg.msg

2021-07-01_20h12_13

For confirmation, the intention is to extract nested mail as an .msg file?

DevPan29 commented 3 years ago

Hi @kenjiuno , yes, now I need to extract the email attached as an .msg file (probably, I'll decide to parse also the attached mail, but for now I don't need it). I would like to manage the attached mail, as a all the other attachments. Infact, I found this problem because the _attached _mail__ exist in the object testMsgInfo.attachments , but if I try to get it through testMsg.getAttachment(index) , I receive the problem above.

Kind regards. Alessandro.

ramod8 commented 3 years ago

@kenjiuno I am also trying to extract the .msg file that is attached and then process it. My use case is a nested .msg file that is an attachment of another .msg file

kenjiuno commented 3 years ago

Ok 1.6.5 will be able to expose embedded msg into another independent msg. https://www.npmjs.com/package/@kenjiuno/msgreader

DevPan29 commented 3 years ago

Great @kenjiuno , I'll try it as soon as possible and I'll back to give you a feedback.

DevPan29 commented 3 years ago

it works perfectly. thank you @kenjiuno