How to List All Folders in a Gmail Account with Python



Python


In this article, we show how to list all folders in a gmail email account with Python.

So, in order to list all folders in a gmail account, we first must gain access to IMAP and authenticate into the email account.

IMAP stands for Internet Message Access Protocol.

IMAP is a protocol that allows a user to gain access into an email account in order to view incoming email or anything within the email account such as folders.

In contrast, SMTP is a protocol that allows a user to gain access into an email account in order to send out email to other people.

So, IMAP is a protocol to view incoming mail, while SMTP is a protocol to send out outgoing email.

In order to view all folders in a gmail account with Python, you need to access IMAP and authenticate into the email account.

So, below is the code in order to access IMAP, authenticate into an email account, and list all folders within the email account.



So, let's go over the code now shown above.

First, we must import the email and imaplib modules.

We then have to specify the IMAP that we want to gain access to, which is gmail. The hosting name for gmail's IMAP is "imap.gmail.com"

We then create a username and password of the gmail account that you are accessing.

Then all you have to do after this to list all folders in the gmail email account is, mail.list()

This will list all folders in your gmail account, including Inbox, All, Important, Spam, Trash, etc.

So, this is just a quick way to check all folders in a gmail email account with Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...