5

I'm looking for a node.js module that can parse my IMAP request - FETCH 1 BODY[TEXT]. I need multipart parser, because I have messages with few levels in hierarchy.

Example of message:

--94eb2c032ec81bf420053483f579
Content-Type: multipart/alternative; boundary=94eb2c032ec81bf411053483f577

--94eb2c032ec81bf411053483f577
Content-Type: text/plain; charset=UTF-8

test

--94eb2c032ec81bf411053483f577
Content-Type: text/html; charset=UTF-8

<div dir="ltr">test</div>

--94eb2c032ec81bf411053483f577--
--94eb2c032ec81bf420053483f579
Content-Type: image/x-icon; name="favicon.ico"
Content-Disposition: attachment; filename="favicon.ico"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_ip2cdokt0

AAABAAEAEA8AAAEAIA... THIS IS ATTACHMENT ...A8AcAAPw/AAA=
--94eb2c032ec81bf420053483f579--)
Axel186
  • 541
  • 5
  • 17

1 Answers1

1

With mailparser we can parse source of e-mail messages into a structured object. It's supports multipart levels - so html/text/attachments will stay in the object and we can find them in attributes.

For example:

const simpleParser = require('mailparser').simpleParser


var f = imap.fetch(results, { bodies: '' })

f.on('message', function (msg, seqno) {
  msg.on('body', async function (stream) {
    const parsed = await simpleParser(stream)

    if (parsed) {
      const fromEmail = parsed.from?.value?.[0]?.address

      if (fromEmail) {
        if (parsed.subject?.length > 0) {
          console.log('parsed.subject :>> ', parsed.subject)
        }

        const lines = parsed.text?.split('\n')
        lines.forEach(function (line, indx) {
          if (line?.length > 0) {
            console.log(`parsed.text[${indx}] :>> `, line)
          }
        })
      } else {
        console.warn('No from email found in', parsed)
      }
    }
  })

  msg.once('end', function () {
    console.log(prefix + 'Finished')
  })
})

Ian
  • 1,746
  • 1
  • 15
  • 28
Axel186
  • 541
  • 5
  • 17
  • 1
    Your question is looking for a MIME parser, not a MIME builder. – Max Oct 05 '16 at 16:03
  • You're right! will update it! – Axel186 Oct 06 '16 at 08:22
  • 1
    this is not an answer?? – Harry Jan 15 '19 at 08:37
  • 1
    Mailparser is what I was looking for and looks like it meets the original question. I would just take out the mime-builder reference and change your example to a mailparser one. – Ian Mar 01 '22 at 20:32
  • 1
    Dear @Ian, Please feel free to edit my question/answer and provide your example there. Or leave a comment here and I will copy/paste that. I am sure people will find it useful! – Axel186 Mar 02 '22 at 09:28
  • It says "Suggested edit queue is full". I do have an example I could share if you can clear out the queue somehow. – Ian Mar 02 '22 at 17:44
  • @Axel186 submitted! – Ian Mar 16 '22 at 14:38