Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to find body of email depending of mimeType

Tags:

gmail-api

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?

like image 834
jgozal Avatar asked May 25 '16 19:05

jgozal


1 Answers

There are many MIME types that can be returned, here are a few:

  • text/plain: the message body only in plain text
  • text/html: the message body only in HTML
  • multipart/alternative: will contain two parts that are alternatives for each othe, for example:
    • a text/plain part for the message body in plain text
    • a text/html part for the message body in html
  • multipart/mixed: will contain many unrelated parts which can be:
    • multipart/alternative as above, or text/plain or text/html as above
    • application/octet-stream, or other application/* for application specific mime types for attachments
    • image/png ot other image/* for images, which could be embedded in the message.

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:

  • the first text/plain or text/html part (either in the message body or in a multipart/mixed)
  • the first text/plain or text/html inside of a multipart/alternative, which may be part of a multipart mixed.

An example of a complex message:

  • multipart/mixed

    • multipart/alternative
      • text/plain <- message body in plain text
      • text/html <- message body in HTML
    • application/zip <- a zip file attachment
  • -
like image 181
Tony BenBrahim Avatar answered Sep 30 '22 04:09

Tony BenBrahim