Open(filepath, "wb").write(part.get_payload(decode=True))īody = msg.get_payload(decode=True).decode() # make a folder for this email (named after the subject)įilepath = os.path.join(folder_name, filename) # print text/plain emails and skip attachmentsĮlif "attachment" in content_disposition: If content_type = "text/plain" and "attachment" not in content_disposition: Subject, encoding = decode_header(msg)įrom, encoding = decode_header(msg.get("From"))Ĭontent_disposition = str(part.get("Content-Disposition"))īody = part.get_payload(decode=True).decode() # parse a bytes email into a message object The N variable is the number of top email messages you want to retrieve, I'm gonna use 3 for now, let's loop over each email message, extract everything we need, and finish our code: for i in range(messages, messages-N, -1): We then converted messages into an integer so we can make a for loop. Messages variable contains a number of total messages in that folder (inbox folder) and status is just a message that indicates whether we received the message successfully. We've used the lect() method, which selects a mailbox (Inbox, spam, etc.), we've chosen the INBOX folder, you can use the imap.list() method to see the available mailboxes. If everything went okay, then you have successfully logged in to your account, let's start getting emails: status, messages = lect("INBOX")
PYTHON EMAIL PARSER CODE
Since I'm testing this on a Gmail account, I've used server, check this link that contains a list of IMAP servers for most commonly used email providers.Īlso, if you're using a Gmail account and the above code raises an error indicating that the credentials are incorrect, make sure you allow less secure apps on your account. We gonna need the clean() function later to create folders without spaces and special characters.įirst, we gonna need to connect to the IMAP server: # create an IMAP4 class with SSL We've imported the necessary modules, and then specified the credentials of our email account. Return "".join(c if c.isalnum() else "_" for c in text)
PYTHON EMAIL PARSER INSTALL
To get started, we don't have to install anything, all the modules used in this tutorial are the built-in ones: import imaplib
PYTHON EMAIL PARSER HOW TO
If you want to read emails with Python using some sort of API instead of the standard imaplib, you can check the tutorial on how to use Gmail API where we cover that out. Unlike the POP3 protocol which downloads emails and deletes them from the server (and then reads them offline), with IMAP, the message does not remain on the local computer, it stays on the server. IMAP is an Internet standard protocol used by email clients to retrieve email messages from a mail server. In this tutorial, you will learn how to use the built-in imaplib module to list and read your emails in Python, we gonna need the help of IMAP protocol.
Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.īeing able to create an application that is able to read your emails and automatically downloading attachments is a handy tool.