0

I'm using python3 and imaplib to retrieve emails from an inbox, so far so good. But these emails are part of a reply chain, and the part payload I get includes the message I reply to as well as the original email content. Is there a way to get only the original content (i.e. if you were viewing the email in gmail's web client, the part of the email before you click ... to view its context)?

import email
import imaplib

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login("xxx@gmail.com", "xxx")
mail.select('inbox')
result, data = mail.search(None, "UNSEEN") # (ALL/UNSEEN)

i = len(data[0].split())
if i > 1:
    print("More than one unread email! Using last one.")
if i == 0:
    print("No unread emails. Will check again soon.")
else:
    result, msg = mail.fetch(data[0].split()[i-1], "(RFC822)")
    msg = email.message_from_bytes(msg[0][1])
    #if msg.is_multipart():
    #   for payload in msg.get_payload():
    #       print(payload.get_payload())
    maintype = msg.get_content_maintype()
    if maintype == "multipart":
        for part in msg.get_payload():
            if part.get_content_maintype() == "text":
                print(part.get_payload())
    elif maintype == "text":
        print(part.get_payload())

This is what I get:

Again reply!

On Mon, Aug 14, 2017 at 6:34 PM, <xxx@gmail.com> wrote:

> Test message
>

<div dir=3D"ltr">Again reply!</div><div class=3D"gmail_extra"><br><div clas=
s=3D"gmail_quote">On Mon, Aug 14, 2017 at 6:34 PM,  <span dir=3D"ltr">&lt;<=
a href=3D"mailto:xxx@gmail.com" target=3D"_blank">xxx@gmail.com=
</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin=
:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Test message<br>
</blockquote></div><br></div>
IronWaffleMan
  • 2,513
  • 5
  • 30
  • 59

0 Answers0