I am making a request to the User.messages endpoint. All objects returned (the emails) have a mimeType property which I'm struggling to understand.
More specifically, I want to be able to extract the body of the email depending of the mimeType since I've been able to notice that depending on the mimeType, the body will be inside the body
property in payload
, or in the parts
array. What are the different mimeTypes that can be returned, and where can I find the body of the email for each one of them?
I know this question is not new but I've wrote a PHP script which correctly parses messages pulled from Gmail API, including any type of attachment.
The script includes a recursive "iterateParts" function which iterates all message parts so we can be sure we extracted all available data from each message.
Script steps are:
This is how $allmsgArr will look like (where only one message was pulled):
There are many MIME types that can be returned, here are a few:
The definitive reference for all this is RFC 2046 https://www.ietf.org/rfc/rfc2046.txt (you might want to also see 2044 and 2045)
To answer your question, build a tree of the message, and look either for:
An example of a complex message:
multipart/mixed
I think it will make sense if you think of the
payload
as apart
in of itself. Let's say I send a message with just a subject and a plain message text:This will result in the following parsed message:
If I send a message with a plain text part, a html part and an image, it will look like this when parsed:
You will see it's just the RFC822-message parsed to JSON. If you just traverse the
parts
, and treat thepayload
as apart
itself, you will find what you are looking for.