Fetching all unseen email (body + attachment) with node-imap
Asked Answered
M

1

0

I am writing a simple software that needs to:

  • Connect to an IMAP server
  • Download all unread messages
  • Store each message's body, and attachment. I prefer the body as text.
  • Mark them as read

I am reading https://github.com/mscdex/node-imap; however, a message can be in multiple parts, and that's where I am lost. For example, when it's multi-part, what is the part that is guaranteed to be the actual email body?

Or, even better, is there a wrapper out there that will just return a nice "message" object, all neatly fetched and prepared for me? Something with the usual headers (from, to, etc.), to body, and a bunch of pre-decoded attachment?

Merc.

Mouseear answered 28/9, 2014 at 0:17 Comment(3)
I wish you could see who votes questions down in SO... at least be polite and write a comment. Bloody hell.Mouseear
This answer will probably help you out: #25247707Aeromarine
Does this answer your question? How to read and save attachments using node-imapIndign
A
1

To answer the good part of your question: No part is guaranteed to be "the email body", you can legally send a message without text. I've seen software that'll send attachment-only mail when the user adds an attachment but leaves the mail body field empty. The way to distinguish between inline bodies and attachments is to look at the content-disposition field, and if that's not present, assume inline for text/* and attachment for other types. (Yes, this also means that there may be more than one inline body. Apple Mail is fond of sending that, for instance.)

The other part of your question is a FAQ and an easy one, and yes, there's much software to build that message object. Approximately 100% of client libraries can do that. Search your documentation for "bodystructure", that's the name of the IMAP fetch item they retrieve and parse in order to build the structure you want.

Azarcon answered 28/9, 2014 at 8:13 Comment(2)
I got it today after much programming. I really don't want to download the lot. So. I am downloading the struct and getting the right part ids. The "good" part of my question was also the main one. Hopefully the downvote wasn't yours.Mouseear
It wasn't mine; your question did contain a worthwhile part.Azarcon

© 2022 - 2024 — McMap. All rights reserved.