Download All Messages & Attachments From Outlook Using Python
Introduction
In this post, I’m going to walk you through how to use Python to download messages and attachments from Outlook. This process is incredibly useful when you want to archive old email conversations and their attachments.
Getting Started
The first thing you need to know is that this method works with any email account linked to Outlook. In my case, I’ve connected my Gmail account, which allows me to retrieve messages from there as well. However, there’s a catch: this method only works on Windows since we’re using the win32com library.
Installation Requirements
To get started, you’ll need to install the pywin32 module. Open your command prompt or terminal and type:
pip install pywin32
Setting Up Your Python Script
Once you have the module installed, create a new Python file. Since we will be dealing with different file paths, import Path from pathlib. This module is part of the standard library, so no additional installation is required. Then, import win32com.client.
Creating an Output Folder
Next, you’ll want to create an output folder for the emails and attachments. I prefer to create this folder in the same working directory as my script. Use mkdir from pathlib to create the folder. Set parents to True to ensure any missing parent directories are also created. If the output folder already exists, you can ignore the error by setting exist_ok to True.
Connecting to Outlook
To connect to Outlook, use:
win32com.client.Dispatch("Outlook.Application")
From this application, you’ll want to get the MAPI namespace, which stands for “Messaging Application Programming Interface.” This allows you to connect to your inbox.
Accessing Messages
Outlook has several default folders, each represented by a number. For example, the inbox is number 6. You can find the numbers for other folders in the official Microsoft documentation.
To get all messages, type:
inbox.Items
Iterating Over Messages
Now that we know which folder we are working in, we can iterate over all messages and extract information like the subject, body, and any attachments. Be careful with the upper and lower case for different properties.
Creating Individual Folders for Each Email
For each message, create a separate folder to save the email body and attachments. Use the subject of the email for the folder name. Then, use mkdir to create this new folder within the output directory.
Saving Email Content
Within the new target folder, create a text file named EMAIL_BODY and write the email body to this file. After that, iterate over the attachments and save each one to the target folder using SaveAsFile.
Final Steps and Execution
Once everything is set up, execute the script. You’ll see an output folder with subfolders for each message. Each subfolder contains the email content and its attachments.
Handling Multiple Email Accounts
You can also connect different email accounts to Outlook. For example, if I have linked my Gmail account, I can access that inbox directly. To do this, specify the folder name in your script. For instance, the folder name could be codingisfun.testuser@gmail.com, and within that, you can connect to the Inbox subfolder.
Conclusion
And that’s it! With these steps, you can easily download messages and attachments from Outlook using Python. If you have any questions or further automation ideas, feel free to leave a comment.