I had a good hard look through the logic in our current implementation found here:
and then had a good hard study of the node-imap package found here:
and then wrote some test Node.js applications just using node-imap and an email server/client.
Although this whole area is new to me, I am not convinced what we have in this node is what we should have. Looking at node-imap, we can ask for a "layout" (structure) of the body of the message. It is either single part or multi-part. In either case, from the structure of the message presented to us, we can determine what parts are what. For any given part of an email we are told its MIME type (type and subtype), encoding and length.
Our current implementation ignores this structure data returned from "node-imap" and instead tries to perform manual string manipulation against the body as a whole by splitting text on "Content-Type".
What I'd like to suggest is a rework of the core structure processing of the email message using the structure map supplied to us by "node-imap". However, this will lead us into potential semantics changes.
Imagine the following circumstances:
1. A plain text only email
2. An HTML text only email
3. A plain text email with one or more attachments
4. An HTML text email with one or more attachments
I believe we just about handle (1) and (2) today. My reading seems to say that for (1), the plain text is placed in msg.payload. We also seem to handle (2) with the HTML text being placed in msg.html (I'm not sure why we didn't put it in msg.payload for consistency).
For (3) and (4) ... things start to go wrong and I believe the code is broken in that if I send in a plain text email with 1 attachment, the "msg.payload" contains neither the plain text email body nor the attachment and the attachment is not to be found anywhere.
What I'd like to offer is the following re-design ... thought through to try and keep existing apps working.
(1) Plain text email only goes to msg.payload
(2) HTML text email only goes to msg.html (would personally have liked it to go to msg.payload ...)
(3) Multi-part emails (which will include the above with 1 or more attachments) will go to an array of msg.payload where each element in the array will be an object still to be designed that includes the MIME type, attachment properties (if any) and the payload of that part.
If we can reach agreement on the above in principle, I'll be happy to write up a more detailed design doc on how it could be implemented. If we agree on that, I'll code it up on a Github branch and issue a pull request.