How to Read Outlook Emails from Specific Folders With Python

Learn how to use python to read Outlook Emails from Specific Folders and Subfolders other than the Inbox

**This Python Outlook Automation uses pywin32 and will only work on Windows PC Devices and Microsoft Outlook email client**

Many people use Outlook folders and subfolders outside of the Inbox to organize different types of emails. Learning how to access specific folders and subfolders other than the Inbox is a great way to automate Outlook tasks more efficiently, since you already know the location of the email. This article will teach you how to read Outlook Emails from specific folders or access a folder’s subfolders using python. The below code uses pywin32 package to find the outlook folder by name and print out details such as sender, subject line, and date details about the last email in the folder.

First, import the win32com.client and os packages.

import win32com.client
import os

#having issues installing win32com? try the below (without the hashtag)

#pip install pypiwin32
#pip install pywin32
#python -m pip install pypiwin32

How to Read Outlook Emails from Specific Folders with pywin32

#Enter Folder Name Below (case insenstive)
folder_name = 'ENTER_FOLDER_NAME_HERE'


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

#If you have multiple mailboxes in outlook, make sure its correct. Try increasing the Item number in the line below
#Example : outlook_mailbox = outlook_application.Folders.Item(2) 
outlook_mailbox =outlook_application.Folders.Item(1) 
print("Reading The Last Email in "+ folder_name +" for: " + str(outlook_mailbox) + '\n')


#finds last email in folder 
folder = outlook_mailbox.Folders(folder_name) 
folder_emails = folder.Items #gets all emails in inbox
folder_emails.Sort("[ReceivedTime]", True) #sorts by last received 
#gets the last email (excludes calendar invites)
last_email = next((e for e in folder_emails if e.MessageClass =='IPM.Note') ,None)

#All Email Information 
email_sender = last_email.Sender #who sent the email
email_recipients = last_email.To #who the email was sent to 
email_date = last_email.ReceivedTime #which day the email was sent
email_subject = last_email.Subject #email subject
email_attachments = len(last_email.attachments) # email attachment count (inline photos count as attachments)
email_body = last_email.Body #email body


#prints out email information 
print("Email Sender: " + str(email_sender))
print("Email Recipients: " + str(email_recipients))
print("Email Date Received: " + str(email_date))
print("Email Subject: " + str(email_subject))
print("Email Attachments: " + str(email_attachments))
print("Email Body: " + str(email_body))

Expected Output:

Reading The Last Email in 'ENTER_FOLDER_NAME_HERE' for: 'YOUR_EMAIL_ADDRESS' 

Email Sender: SENDER_NAME
Email Recipients: RECIPIENT_NAME
Email Date Received: 2022-09-03 10:06:08.482000+00:00
Email Subject: EMAIL_SUBJECT
Email Attachments:  ATTACHMENT_COUNT
Email Body: EMAIL_BODY

How To Read Outlook Emails from a Subfolder using pywin32

Need a different folder within a folder? Accessing an Outlook subfolder using Python is just as easy as accessing a folder! You just need to know the name of the parent folder and name of the subfolder.

#Enter Folder Name Below (case insenstive)
folder_name = 'ENTER_FOLDER_NAME_HERE'
sub_folder_name = 'ENTER_SUBFOLDER_NAME_HERE'


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

#If you have multiple mailboxes in outlook, make sure its correct. Try increasing the Item number in the line below
#Example : outlook_mailbox = outlook_application.Folders.Item(2) 
outlook_mailbox =outlook_application.Folders.Item(1)
print("Reading The Last Email in Subfolder "+ sub_folder_name +" for: " + str(outlook_mailbox) + '\n')


#finds last email in folder 
folder = outlook_mailbox.Folders(folder_name)
sub_folder = folder.Folders(sub_folder_name)
sub_folder_emails = sub_folder.Items #gets all emails in inbox
sub_folder_emails.Sort("[ReceivedTime]", True) #sorts by last received 
#gets the last email (excludes calendar invites)
last_email = next((e for e in sub_folder_emails if e.MessageClass =='IPM.Note') ,None)

#All Email Information 
email_sender = last_email.Sender #who sent the email
email_recipients = last_email.To #who the email was sent to 
email_date = last_email.ReceivedTime #which day the email was sent
email_subject = last_email.Subject #email subject
email_attachments = len(last_email.attachments) # email attachment count (inline photos count as attachments)
email_body = last_email.Body #email body


#prints out email information 
print("Email Sender: " + str(email_sender))
print("Email Recipients: " + str(email_recipients))
print("Email Date Received: " + str(email_date))
print("Email Subject: " + str(email_subject))
print("Email Attachments: " + str(email_attachments))
print("Email Body: " + str(email_body))

Expected Output:

Reading The Last Email in 'ENTER_SUBFOLDER_NAME_HERE' for: 'YOUR_EMAIL_ADDRESS' 

Email Sender: SENDER_NAME
Email Recipients: RECIPIENT_NAME
Email Date Received: 2022-09-03 10:06:08.482000+00:00
Email Subject: EMAIL_SUBJECT
Email Attachments:  ATTACHMENT_COUNT
Email Body: EMAIL_BODY

Congratulations! You now know a fundamental skill for Python Outlook Automation tasks – reading emails from specific folders and subfolders. We hope this code helps you automate your workflow and helps you take on your next project with confidence. What will YOU do with your extra time automating Outlook tasks with Python will save?

Need to access something other than the last email? Check out How to Loop Through Outlook Emails with Python next!


Comments

One response to “How to Read Outlook Emails from Specific Folders With Python”

  1. I am sure thius article hhas touched aall thhe intwrnet people, itss
    realpy rewally pleasan post on builfing uup neww website.

Leave a Reply

Your email address will not be published. Required fields are marked *