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 –
- Fetch Unseen Emails From Gmail Inbox
- Fetch top 10 starred repositories of user on GitHub | Python
- Gmail Login using Python Selenium
- Send mail from your Gmail account using Python
- Send mail with attachment from your Gmail account using Python
- Fetching recently sent mails details sent via a Gmail account using Python
- How to fetch data from MongoDB using Python?
- Python | Fetch Nearest Hospital locations using GoogleMaps API
- Python program to fetch the indices of true values in a Boolean list
- Sending Emails Using API in Flask-Mail
- Python Tweepy - Getting the name of a user
- Python Tweepy – Getting the URL of a user
- Python Tweepy - Getting the ID of a user
- Take Matrix input from user in Python
- Python Tweepy - Getting the location of a user
- Add a User in Linux using Python Script
- Python | Get a list as input from user
- Python Tweepy – Getting the description of a user
- Python Tweepy - Getting the screen name of a user
- Python User defined functions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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