0

im trying to fetch the body of emails from a gmail account using imaplib.

Here's the code i've come up with:

import imaplib
import email

user = '********@gmail.com'
password = '*********'
imap_url = 'imap.gmail.com'


def get_body(msg):

    if msg.is_multipart:

        return get_body(msg.get_payload(0))

    else:

        msg.get_payload(None, True)


con = imaplib.IMAP4_SSL(imap_url)

con.login(user, password)

con.select('INBOX')

result, data = con.fetch(b'1', '(RFC822')    #b'1' to get most recent
raw = email.message_from_bytes(data[0][1])

print(get_body(raw))

However i get this error message:

raise self.abort('socket error: EOF')
imaplib.abort: socket error: EOF

Not sure what isnt working right since I've used con.list() to verify 'INBOX' and this account has two emails in it at the moment, so it is not empty. Thank you in advance if you can help!

GSatterwhite
  • 301
  • 1
  • 3
  • 12

0 Answers0