In this article, we will see how to read Emails from your Gmail using Gmail API in Python. Gmail API is a RESTful API that allows users to interact with your Gmail account and use its features with a Python script.
So, let’s go ahead and write a simple Python script to read emails.
- Python (2.6 or higher)
- A Google account with Gmail enabled
- Beautiful Soup library
- Google API client and Google OAuth libraries
Install the required libraries by running these commands:
pip install –upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
Run this to install Beautiful Soup:
pip install beautifulsoup4
Now, you have to set up your Google Cloud console to interact with the Gmail API. So, follow these steps:
- Sign in to Google Cloud console and create a New Project or continue with an existing project.
- Go to APIs and Services.
- Enable Gmail API for the selected project.
- Now, configure the Consent screen by clicking on OAuth Consent Screen if it is not already configured.
- Enter the Application name and save it.
- Now go to Credentials.
- Click on Create credentials, and go to OAuth Client ID.
- Choose application type as Desktop Application.
- Enter the Application name, and click on the Create button.
- The Client ID will be created. Download it to your computer and save it as credentials.json
Please keep your Client ID and Client Secrets confidential.
Now, everything is set up, and we are ready to write the code. So, let’s go.
The file ‘token.pickle‘ contains the User’s access token, so, first, we will check if it exists or not. If it does not exist or is invalid, our program will open up the browser and ask for access to the User’s Gmail and save it for next time. If it exists, we will check if the token needs to be refreshed and refresh if needed.
Now, we will connect to the Gmail API with the access token. Once connected, we will request a list of messages. This will return a list of IDs of the last 100 emails (default value) for that Gmail account. We can ask for any number of Emails by passing an optional argument ‘maxResults‘.
The output of this request is a dictionary in which the value of the key ‘messages‘ is a list of dictionaries. Each dictionary contains the ID of an Email and the Thread ID.
Now, We will go through all of these dictionaries and request the Email’s content through their IDs.
This again returns a dictionary in which the key ‘payload‘ contains the main content of Email in form of Dictionary.
This dictionary contains ‘headers‘, ‘parts‘, ‘filename‘ etc. So, we can now easily find headers such as sender, subject, etc. from here. The key ‘parts‘ which is a list of dictionaries contains all the parts of the Email’s body such as text, HTML, Attached file details, etc. So, we can get the body of the Email from here. It is generally in the first element of the list.
The body is encoded in Base 64 encoding. So, we have to convert it to a readable format. After decoding it, the obtained text is in ‘lxml‘. So, we will parse it using the BeautifulSoup library and convert it to text format.
At last, we will print the Subject, Sender, and Email.
Now, run the script with
This will attempt to open a new window in your default browser. If it fails, copy the URL from the console and manually open it in your browser.
Now, Log in to your Google account if you aren’t already logged in. If there are multiple accounts, you will be asked to choose one of them. Then, click on the Allow button.
After the authentication has been completed, your browser will display a message: “The authentication flow has been completed. You may close this window”.
The script will start printing the Email data in the console.
You can also extend this and save the emails in separate text or csv files to make a collection of Emails from a particular sender.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.