title: Read email in python 3.7 using imaplib with HTML body and attachments in the email
body: <p>I would really appreciate if someone can help me with this issue.</p>
  
  <p>I have implemented the below code to read "unread emails from gmail inbox". I need to print "To", "From", "Subject", "Body" and "save attachments in a specified location"</p>
  
  <p>I have 2 issues here.</p>
  
  <ol>
  <li>If there is any email with attachments, it gives the error <code>Body:     [&lt;email.message.Message object at 0x026D1050&gt;, &lt;email.message.Message object at 0x02776B70&gt;]</code>. It will print all the required things and saves attachments but DOESN'T print the body.</li>
  </ol>
  
  <p>This works fine if no attachment is included.</p>
  
  <ol start="2">
  <li>If there is an email body with any styling in it like "bold/italic/underline/colour...etc", it doesn't print as it is.</li>
  </ol>
  
  <p>Example : <strong>Python</strong>  is printed as Python=C2=A0i= and sometimes different styling is seperated by "*". </p>
  
  <pre><code>def get_body(email_message):
  for payload in email_message.get_payload():
       # print('Body:\t', payload.get_payload())
       break
  return(payload.get_payload())
  def read_email(server,uname,pwd):
      username = uname
      password = pwd
      mail = imaplib.IMAP4_SSL(server)
      mail.login(username, password)
      mail.select("inbox")
      try:
          result, data = mail.uid('search', None, '(UNSEEN)')
          inbox_item_list = data[0].split()
          most_recent = inbox_item_list[-1]
          result2, email_data = mail.uid('fetch', most_recent, '(RFC822)')
          raw_email = email_data[0][1].decode("UTF-8")
          email_message = email.message_from_string(raw_email)
          for part in email_message.walk():
              if part.get_content_maintype() == 'multipart':
                  continue
              if part.get('Content-Disposition') is None:
                  continue
              filename = part.get_filename()
              att_path = os.path.join(location, filename)
  
              if not os.path.isfile(att_path):
                  fp = open(att_path, 'wb')
                  fp.write(part.get_payload(decode=True))
                  fp.close()
                  print('Downloaded file:', filename)
          if email_message.is_multipart():
              for payload in email_message.get_payload():
                  print('To:\t\t', email_message['To'])
                  print('From:\t',     email_message['From'])
                  print('Subject:', email_message['Subject'])
                  print('Date:\t',email_message['Date'])
                  print('Body:\t', get_body(email_message))
                  break        
          else:
              print('Nothing'])               
      except IndexError:
          print("No new email")
  while True:
      read_email("imap.gmail.com", "s@gmail.com", "spassword")
  time.sleep(10)
  </code></pre>
  
  <p>Many thanks </p>