Search code examples
pythonimaplib

imaplib reading incorrect email


I'm trying to have my program search for a specific item then read the email.

However, when I try to print its subject, it instead gives the subject of a completetly different email, even though I have designated it to first only search for emails with the specific subject.

con = imaplib.IMAP4_SSL(imap_url)
con.login(user,password)

con.select('INBOX')

result, data = con.fetch(b'1','RFC822')
status, message = con.search(None, '(SUBJECT "ID: BY8943N7435GO || Please confirm your team registration")')

list_emails = message[0].split()


most_recent = list_emails[-1]


result2, email_data = con.uid('fetch',list_emails[-1],'(RFC822)')


raw_email = email_data[0][1].decode("utf-8")

email_message = email.message_from_string(raw_email)

print(email_message['Subject'])

Solution

  • i was able to fix it by replacing the uid fetch to the following:

    result2, email_data = con.fetch(list_emails[-1],'(RFC822)')
    

    opposed to:

    result2, email_data = con.uid('fetch',list_emails[-1],'(RFC822)')