Python is a widely used high-level, general-purpose, interpreted, multi-utility, dynamic programming language. It can be used to do a wide range of tasks like machine learning, web application development, cross-platform GUI development, and much more. Fetching Gmail is another of a task that could be achieved by Python. You may need to fetch a mail from your inbox in your project or website for any reason. In this tutorial, we’ll learn about how to fetch unseen emails from Gmail Inbox/Sent Mails.
To begin, we will be required an app password that is generated by Google Security, since using Password cannot be secure using in some projects or scripts directly.
How to Generate App Password :
- Go to account.google.com with your account.
- In left tabs, choose Security.
- Under Signing into Google, Choose App Password.
- Confirm your identity.
- Choose Custom.
- Choose any name for your Application and Generate the Password.
- That’s your App Password. Copy the password, we’ll be needing that.
You’ll also need to Enable IMAP in the Google Settings.
The Library you’ll use are :
That’s all for the prerequisite. Let’s see the code now.
Output: This code will fetch you the top 20 unseen emails in the inbox.
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 ?
- Python | Fetch your gmail emails from a particular user
- Sending Emails Using API in Flask-Mail
- Send mail from your Gmail account using Python
- Send mail with attachment from your Gmail account using Python
- Gmail Login using Python Selenium
- Fetching recently sent mails details sent via a Gmail account using Python
- Fetch top 10 starred repositories of user on GitHub | 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?
- Building Blog CMS (Content Management System) with Django
- Python - List Comprehension
- Python List Slicing
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.