imaplib: what factors decide maintype is "text" or "multipart"
<p>I have been writing python code for small tool, wherein i am trying to fetch mails using  python libraries imaplib and email.
code statement is something like below.</p>

<pre><code>import imaplib
import email
mail = imaplib.IMAP4_SSL('imap.server')
mail.login('userid@mail.com', 'password')
result, data = mail.uid('search', None, "ALL")
latest_email_uid = data[0].split()[-1]
result, data = mail.uid('fetch', latest_email_uid, '(RFC822)')
raw_email = data[0][1]
email_message = email.message_from_string(raw_email)
maintype = email_message_instance.get_content_maintype()
</code></pre>

<p>I am executing the script from different host machines simultaneously.
Problem that I am facing is, while fetching the mail body, for <strong>same incoming email</strong>, on first host mac maintype is evaluated as "text" whereas for other host machine, its evaluated as "multipart" during script execution.</p>

<p>Would like to know how these values are determined at runtime and if I always want maintype to be "multipart", what standard layout should I follow while writing email in email body.</p>

<p>From comments:</p>

<blockquote>
  <p>raw_email for both cases has raw html code with multiple values. all most all html code is same except few differences. For maintype=multipart, Content-Type="multipart/alternative", boundary tag is present. For maintype=text, Content-Type="text/html", boundary field is not present </p>
</blockquote>

<p>Well, that answers the question. <code>get_content_maintype</code> returns first part of the <code>Content-Type</code>, which is <code>multipart</code> for <strong>multipart</strong>/alternative and <code>text</code> for <strong>text</strong>/html.</p>

<p><code>multiplart/alternative</code> means there are multiple alternative versions of the email. Usually, that is html + text. Emails are often sent that way, because then they can be read by any client (the text part), but will still contain HTML formatting which will be used in clients which support it.</p>

<p>Apparently one of the emails was sent with both html and text, whereas the other contains only html.</p>

python email imaplib