By: Bhagesh Kumar
Topic.
Hello Everyone, Hope you are fine. In this blog we will talk about Outlook mail scraping in python using pywin32 library, which is actually reading emails using python. Nowadays, Automation is growing day by day due to the advancement in technology and programming languages support. Its better idea to read most common emails which are coming every day containing some useful information like product rates, stock rates or coin rates and we have to keep tracking on them multiple times on a single working day. So let’s drop this burden from our head and put it on a python.
Now let’s do some email automation with python.
There are many automation applications available now a days and most of them are using C and Python as base language. I am using Python to do this Automation of Reading Emails. This program will not only read emails automatically for you from outlook, but it could also perform some processing which will be discussed in upcoming blogs. In this blog I will be discussing about the accessing of email items through Python code. So continue reading, you will find the article very useful for you.
At every step I will explain you the code. Before moving further, let’s install some dependencies.
Environment:
Python 3.x
Outlook Application
Jupyter Notebook
pywin32 Library
win32.client
I hope that you have successfully configured and installed all dependencies and libraries. If you want to access the native outlook application you must need the above pywin32 Library. If you have not installed then don’t worry it can be done by Python PIP command.
pip install pywin32
Now I am importing this library and other basic imports such as os, datetime
#import installed python libraries
import win32com.client
import os
from datetime import datetime
Let’s get hands dirty.
We have imported all libraries successfully. You wonder that, how to access the outlook application or any email app by coding. So, my dear everything is possible in today’s I.T world. For communication with outlook we need to create the session in the first place and also connect outlook by MAPI with the help of GetNamespace Function. By calling that it will provide the outlook session for further operation. Let’s see this in code for clear understanding.
#creating a session with outlook application
outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")
Now For accessing the Inbox folder we have to pass the code which is 6, every folder has its own code. You can also check the documentation from the below given link of other folder such as Outbox Sent, Draft, Deleted Items etc.
Take a look on this by code given below:
#Accessing the inbox by passing the inbox code 6
inbox = mapi.GetDefaultFolder(6)
We have get access the inbox folder, further we want to view messages by getting inbox items with the help of below code.
#Retreaving the message items
messages = inbox.Items
As we get the items of message, now we can filter the message by getting the receiving date, last message, subject, sender email address and more. We can iterate the messages and take unread message subject, sender.
For better understanding look the code below how we can filter the message of inbox. The message has a lot properties from which we find sender, sender email, sent on, sentondate, sentontime etc. you will understand the functionality of every command by reading the commented lines in the below code.
#Iterating the message items and reading unread emails
for message in messages:
if message.UnRead == True:
#getting the subject of message:
subject = message.Subject
#getting the sender of message:
sender = message.Sender
#getting the email address of sender
sndremail = sender.GetExchangeUser().PrimarySmtpAddress
#printing the email address of sender
sndremail = sender.GetExchangeUser().PrimarySmtpAddress
#printing the all data
print("Subject",subject)
print("Sender",sender)
print("Sender Email Address",sndremail)
#get the first email
message = messages.GetFirst()
#get the last email
message = messages.GetLast()
You can check the different attributes of email by running the below code one by one.
message.subject
message.senton
message.senton.date()
message.senton.time()
message.sender
message.SenderEmailAddress
There are also many other attributes which you can check from the official Microsoft documentation.
Leave comment in comment section below.