If you are ever curious to know how we can fetch Gmail e-mails using Python then this article is for you.
As we know Python is a multi-utility language which can be used to do a wide range of tasks. Fetching Gmail emails though is a tedious task but with Python, many things can be done if you are well versed with its usage. Gmail provides IMAP access to clients who want to access Gmail without manually logging in the browser.
In setting page, enable this before running script.
The libraries used in this implementation includes imaplib, email. You have to manually go and make IMAP access enabled by going into your Gmail account settings. After this only you could access your Gmail account without logging in browser.
- Three functions are defined in the implementation which is used to get email body, search for emails from a particular user and get all emails under a label.
- For showing results I have sent email to my id from my another Gmail account. Now I will be fetching emails from my Gmail account which is received from my another Gmail account.
- The process begins from making Gmail connection with the help of imaplib library and proving our Gmail login credentials to it.
- After logging we are selecting emails under the label: Inbox which is a default labeled section for all users. However, you can create your own labels also.
- Then we are calling get emails function and provide it the parameter from search function result i.e “from user”
- In get emails function we are putting all emails in an array named “msgs”
- Now print to see the msgs array
- Now we can easily iterate over this array. We are iterating it in the order the emails arrived. Then we are searching for the index from where our content begins. This indexing part will be different for different emails/users and the user can manually change the indexes to print only that part which they require.
- We have our results printed out.
Below is the Python implementation –
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.
- How to read Emails from Gmail using Gmail API in Python ?
- Fetch Unseen Emails From Gmail Inbox
- Send mail from your Gmail account using Python
- Send mail with attachment from your Gmail account using Python
- Fetch top 10 starred repositories of user on GitHub | Python
- Gmail Login using Python Selenium
- Fetching recently sent mails details sent via a Gmail account using Python
- Sending Emails Using API in Flask-Mail
- Web scraper for extracting emails based on keywords and regions
- Change your way to put logic in your code - Python
- Python | Fetch Nearest Hospital locations using GoogleMaps API
- Python program to fetch the indices of true values in a Boolean list
- How to fetch data from MongoDB using Python?
- Python | Count of elements matching particular condition
- Python | Split list into lists by particular value
- Python | Get values of particular key in list of dictionaries
- Python | Count keys with particular value in dictionary
- Python | Remove particular element from tuple list
- Python | Get elements till particular element in list
- Python | Group strings at particular element in list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai