Codementor Events

How to Read Outlook Emails by Python

Published May 16, 2018Last updated Nov 12, 2018
How to Read Outlook Emails by Python

when you start e-mail marketing , You need opt-in email address list. You have opt-in list. You are using email client software and If you can export your list from your email client, You will have good list.

Now I am trying to explain my codes to write all emails into test file from your outlook profile.

First you should import win32com.client, You need to install pywin32

pip install pywin32

We should connect to Outlook by MAPI

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

Then we should get all accounts in your outlook profile.

accounts= win32com.client.Dispatch("Outlook.Application").Session.Accounts;

Then You need to get emails from inbox folder that is named emailleri_al.

def emailleri_al(folder):
    messages = folder.Items
    a=len(messages)
    if a>0:
        for message2 in messages:
             try:
                sender = message2.SenderEmailAddress
                if sender != "":
                    print(sender, file=f)
             except:
                print("Ben hatayım")
                print(account.DeliveryStore.DisplayName)
                pass

             try:
                message2.Save
                message2.Close(0)
             except:
                 pass


You should go to all account and get inbox folder and get emails

for account in accounts:
    global inbox
    inbox = outlook.Folders(account.DeliveryStore.DisplayName)
    print("****Account Name**********************************",file=f)
    print(account.DisplayName,file=f)
    print(account.DisplayName)
    print("***************************************************",file=f)
    folders = inbox.Folders

    for folder in folders:
        print("****Folder Name**********************************", file=f)
        print(folder, file=f)
        print("*************************************************", file=f)
        emailleri_al(folder)
        a = len(folder.folders)

        if a>0 :
            global z
            z = outlook.Folders(account.DeliveryStore.DisplayName).Folders(folder.name)
            x = z.Folders
            for y in x:
                emailleri_al(y)
                print("****Folder Name**********************************", file=f)
                print("..."+y.name,file=f)
                print("*************************************************", file=f)

All Code is as the following

import win32com.client
import win32com
import os
import sys

f = open("testfile.txt","w+")

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
accounts= win32com.client.Dispatch("Outlook.Application").Session.Accounts;

def emailleri_al(folder):
    messages = folder.Items
    a=len(messages)
    if a>0:
        for message2 in messages:
             try:
                sender = message2.SenderEmailAddress
                if sender != "":
                    print(sender, file=f)
             except:
                print("Error")
                print(account.DeliveryStore.DisplayName)
                pass

             try:
                message2.Save
                message2.Close(0)
             except:
                 pass



for account in accounts:
    global inbox
    inbox = outlook.Folders(account.DeliveryStore.DisplayName)
    print("****Account Name**********************************",file=f)
    print(account.DisplayName,file=f)
    print(account.DisplayName)
    print("***************************************************",file=f)
    folders = inbox.Folders

    for folder in folders:
        print("****Folder Name**********************************", file=f)
        print(folder, file=f)
        print("*************************************************", file=f)
        emailleri_al(folder)
        a = len(folder.folders)

        if a>0 :
            global z
            z = outlook.Folders(account.DeliveryStore.DisplayName).Folders(folder.name)
            x = z.Folders
            for y in x:
                emailleri_al(y)
                print("****Folder Name**********************************", file=f)
                print("..."+y.name,file=f)
                print("*************************************************", file=f)



print("Finished Succesfully")
Discover and read more posts from A.A. Cetrefli
get started
post comments14Replies
Sai Reddy
4 years ago

Hi, I was looking for python code which can read emails and parse the email address from that email and store it into excel.
Anybody have any insights please guide me.

Raghu Periyasamy
4 years ago

Hi Sai Reddy, I am looking the same. Did you get any update on this ?

Sai Reddy
4 years ago

Not yet. I’m still looking into it.

Ana Neto
3 years ago

If the Outlook email comes from an Exchange Server AND you have access to the Exchange Server, I would rather connect via the Server and from there to Excel or a database or whatever you want. This way you would not be dependent on the client (Outlook) https://www.connecting-software.com/blog/how-to-read-email-from-exchange-server-python-tutorial/

sourabh mittal
4 years ago

Hi, I appreciate this work. I am looking for some more help on how to restrict mails to some timeperiod only. Like for last month or so.

JG
4 years ago

This was very helpful in getting me started working with Outlook. So in return let me point out a couple places where it can be improved.
First, in Python, len() only works on iterables and returns a zero if the sequence is empty. So there is nothing gained by not just running the for loop directly on the objects. It will error where len() would error, and not run where len() is zero.
Second, this code only looks at two levels of Outlook folders when there can be any number of subfolders (well I’ve seen 300 as the level limit;-). This calls for a recursive solution. The bulk of the code could be replaced with:

def scanFolderAndSubfolders(folder):

emailleri_al(folder)
for subfolder in folder.Folders :
    scanFolderAndSubfolders(subfolder)

for account in accounts:
scanFolderAndSubfolders(outlook.Folders(account.DeliveryStore.DisplayName))

Show more replies