How to retrieve messages from Gmail using Python

In this tutorial and subsequent few tutorials, we will explore Test Automation with regard to Gmail. In this tutorial, we will explore how to retrieve messages from Gmail using Python. We have covered How to send email through Gmail in Python here.

Example : Whether, we like it or not, we receive a number of emails. Most of these emails are mostly from newsletter subscriptions, promotions and so on. I thought, it would be easy to retrieve some ( or all ) emails based on certain criterion and delete it, mark it as spam and so on.

Step 1 : Login to Gmail

We use the Standard Python library imaplib to retrieve emails from Gmail. So the first step is to connect to the Gmail server  imap.gmail.com. You can use the following code

If you are not able to login because the login credentials are not proper, it would raise an exception. I would assume, the user handle the exception due to wrong credentials.


Step 2 : List all the Folders

You can list all the folders in your Gmail using .list()


Step 3 : Select the Folder

You can parse each of the folders in all_folders and retrieve emails all the folders or, if you know the folder name, you can retrieve the emails from this particular folder alone. In this example, I would like to retrieve all the emails from folder titled “Sapnaedu”


Step 4 : Retrieve emails based on Filter Criterion

In this example, let us retrieve all the emails sent from the email address contact@sapnaedu.in in the last 3 days. It is important you know the basic filters while filtering the emails based on certain criterion. For instance, following are the basic filters :

Description Filter
 Retrieve All the emails from contact@sapnaedu.in  From:sapnaedu.in
 Retrieve All the emails from contact@sapnaedu.in having attachment  From:sapnaedu.in has:attachment
 Retrieve All the emails from contact@sapnaedu.in since May 01, 2016  From:sapnaedu.in after:2016/05/01
 Retrieve All the emails from contact@sapnaedu.in has subject Thanks  From:sapnaedu.in subject:Thanks

You can build different filters for Gmail. You can read about different filters here. The following code gives the message ID of all the emails matching our criterion.


Step 5 : Retrieve email content

Now, the final step is to parse through each Message ID of the email matching our search criterion

Description Filter
 Retrieve All the emails from contact@sapnaedu.in  From:sapnaedu.in
 Retrieve All the emails from contact@sapnaedu.in having attachment  From:sapnaedu.in has:attachment
 Retrieve All the emails from contact@sapnaedu.in since May 01, 2016  From:sapnaedu.in after:2016/05/01
 Retrieve All the emails from contact@sapnaedu.in has subject Thanks  From:sapnaedu.in subject:Thanks

You can build different filters for Gmail. You can read about different filters here. The following code gives the message ID of all the emails matching our criterion.

 

Step 6 : Process each email

The final step is to parse the email. You can parse the Subject, content to mark the email as Spam, or to delete the email altogether.

Download

The complete Python source can be downloaded from here. Please note, that the code is tested using Python 2.7 . If you are using Python 3.3, you might have to do some minor changes in the code.

I hope, you find this article very useful. If you have any doubts or have any comments concerning this tutorial, Please leave a comment or contact me. I would be glad to help.




Kiran Chandrashekhar

Hey, Thanks for dropping by. My name is Kiran Chandrashekhar. I am a full-time software freelancer. I love Maths and Mathematical Shortcuts. Numbers fascinate me. I will be posting articles on Mathematical Shortcuts, Software Tips, Programming Tips in this website. I love teaching students preparing for various competitive examinations. Read my complete story.

Leave a Reply

Your email address will not be published. Required fields are marked *

Protected by WP Anti Spam