jstedfast / gmime

A C/C++ MIME creation and parser library with support for S/MIME, PGP, and Unix mbox spools.
GNU Lesser General Public License v2.1
113 stars 36 forks source link

No plain part if the Content-Type is multipart/alternative #85

Closed Jeevhi closed 4 years ago

Jeevhi commented 4 years ago

I am using gmime to decode the message body. I am not sure if it's a bug or that's how it should be, but I am unable to retrieve plain part from the message body. The Iterator only returns one part ( html part) even though both parts are present in the message. However, if the context type is multipart/mixed (with attachment), Iterator returns all the parts (plain, html, attachment).

GMimeObject *mime = g_mime_message_get_mime_part(message);
GMimePartIter *iter = g_mime_part_iter_new (mime);

while (g_mime_part_iter_next (iter)) {
...
}

Below is the multipart-message

Content-Type: multipart/alternative;
 boundary="b1_Y2t1lEr6GADYZN0KffJwRMAB2lWd3ExEuRJKjCFRQ4"
Content-Transfer-Encoding: 8bit

This is a multi-part message in MIME format.
--b1_Y2t1lEr6GADYZN0KffJwRMAB2lWd3ExEuRJKjCFRQ4
Content-Type: text/plain; charset=us-ascii

This is the body in plain text for non-HTML mail clients

--b1_Y2t1lEr6GADYZN0KffJwRMAB2lWd3ExEuRJKjCFRQ4
Content-Type: text/html; charset=us-ascii

This is the HTML message body <b>in bold!</b>

--b1_Y2t1lEr6GADYZN0KffJwRMAB2lWd3ExEuRJKjCFRQ4--
jstedfast commented 4 years ago

After g_mime_part_iter_new (mime) - the g_mime_part_iter_get_current() will return the first part of the multipart.

Once you call g_mime_part_iter_next(), it iterates to the next part, which is the html part.

TL;DR

You need to use a do-while loop, not a while loop.