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
1
2
3
4
5
6
|
conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
gmail_user = 'kiran.chandrashekhar'
gmail_pwd = 'gmail_password_here'
conn.login(gmail_user, gmail_pwd)
conn.select()
|
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()
1
|
status, all_folders = conn.list() #Check status for 'OK'
|
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”
1
2
3
|
folder_to_search = '[Google Mail]/Sapnaedu'
status, select_info = server.select_folder(folder_to_search) #Check status for 'OK'
|
Step 4 : Retrieve emails based on Filter Criterion
In this example, let us retrieve all the emails sent from the email address [email protected] 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 [email protected] | From:sapnaedu.in |
Retrieve All the emails from [email protected] having attachment | From:sapnaedu.in has:attachment |
Retrieve All the emails from [email protected] since May 01, 2016 | From:sapnaedu.in after:2016/05/01 |
Retrieve All the emails from [email protected] 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.
1
2
3
4
5
6
7
8
|
today = datetime.today()
cutoff = today - timedelta(days=3) #Retrieve all the emails since last 3 days
search_key = from_email + " after:" + cutoff.strftime('%Y/%m/%d')
status, message_ids = conn.search(None, 'X-GM-RAW', search_key)
|
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 [email protected] | From:sapnaedu.in |
Retrieve All the emails from [email protected] having attachment | From:sapnaedu.in has:attachment |
Retrieve All the emails from [email protected] since May 01, 2016 | From:sapnaedu.in after:2016/05/01 |
Retrieve All the emails from [email protected] 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
for id in message_ids[0].split():
status, data = conn.fetch(id, '(RFC822)')
email_msg = email.message_from_string(data[0][1])
#Print all the Attributes of email message like Subject,
#print email_msg.keys()
subject = email_msg['Subject']
sender_email = email_msg['From']
sent_to_email = email_msg['To']
for part in email_msg.walk():
if part.get_content_type() == 'text/plain':
email_content = part.get_payload() # prints the raw text
#TODO :
#process_email() #Delete, Mark as Spam, Forward it
#TODO :
#print email_content
|
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.