Browse 536 attributed reviews, viewable separately for our classroom and online training
Reading Outlook emails and downloading attachments using Python
This blog shows how you can use Python to loop over all of the emails in a folder in Outlook, saving the attachments for each to your hard disk.

Posted by Andy Brown on 08 October 2021

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Reading Outlook emails and downloading attachments using Python

Is there any limit to what you can do easily in Python?  This blog explains how you can loop over the emails in an Outlook folder, downloading all the attachments (although it contains lots of other hints and tips on working with Outlook from within Python).

This blog assumes that you have a reasonable knowledge of Python.  It was written using Visual Studio Code in Windows 10.  The best source reference page I found for working with Outlook in Python is this StackOverflow question.

Installing the necessary modules

To get this to work, you'll need to install the win32com.client module:

Installing pywin32

How to do this in Visual Studio Code.

 

If you still get missing module messages, try some of the ideas in my StackOverflow answer.

Getting the numbers of the Outlook folders

To make my code easier to read, I've used a Python enumeration to get a list of the main mail folders:

from enum import Enum

class OutlookFolder(Enum):

olFolderDeletedItems = 3 # The Deleted Items folder

olFolderOutbox = 4 # The Outbox folder

olFolderSentMail = 5 # The Sent Mail folder

olFolderInbox = 6 # The Inbox folder

olFolderDrafts = 16 # The Drafts folder

olFolderJunk = 23 # The Junk E-Mail folder

This isn't a complete list, but does contain the mail folders you're most likely to be working with.

Getting a reference to your folder

The next thing to do is to get a reference to the mail folder that you want to use:

# the module to work with

import win32com.client as win32

# get a reference to Outlook

outlook = win32.Dispatch("Outlook.Application").GetNamespace("MAPI")

# get the Inbox folder (you can a list of all of the possible settings at https://docs.microsoft.com/en-us/office/vba/api/outlook.oldefaultfolders)

inbox = outlook.GetDefaultFolder(OutlookFolder.olFolderInbox.value)

# get subfolder of this

todo = inbox.Folders.Item("To do - home")

Here I'm going to look at the emails in this folder, which is a subfolder of my inbox:

The folder to look at

I have a lot of to do lists!

 

Looping over the messages in this folder

The rest of my code reads the details for each message in the folder into a tuple, adds this tuple to a list and then loops over the list processing each message in turn:

# get all the messages in this folder

messages = todo.Items

# check messages exist

if len(messages) == 0:

print("There aren't any messages in this folder")

exit()

# loop over them all

emails = []

for message in messages:

# get some information about each message in a tuple

this_message = (

message.Subject,

message.SenderEmailAddress,

message.To,

message.Unread,

message.Senton.date(),

message.body,

message.Attachments

)

# add this tuple of info to a list holding the messages

emails.append(this_message)

# show the results

for email in emails:

# unpack the tuple to get at information

subject, from_address, to_address, if_read, date_sent, body, attachments = email

# show the subject

print(subject, to_address)

# number of attachments

if len(attachments) == 0:

print("No attachments")

else:

for attachment in attachments:

attachment.SaveAsFile("c:\\__ajb\\" + attachment.FileName)

print("Saved {0} attachments".format(len(attachments)))

Remarkably, this simple routine saves all of the attachments for messages in the given folder to a folder on my hard disk! 

This blog has 0 threads Add post