Learn how to use python to get and read Outlook Emails with a specific subject
**This Python Outlook Automation uses pywin32 and will only work on Windows PC Devices and Microsoft Outlook email client**
Learning how to access specific emails threads using the subject line is a great way to automate Outlook tasks more efficiently, since you already know some criteria about the email. This article will teach you how to find and return an email from your inbox, or a specific folder using just the subject line. The below code uses pywin32 package to find the an email by subject line 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 Get and Read an Outlook Email by Subject Line using pywin32
#Enter Folder Name Below (case insenstive) folder_name = 'ENTER_FOLDER_NAME_HERE' subject_line = 'ENTER_SUBJECT_LINE_HERE' #finds anything that contains report - case insensitive 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 that contains "+ subject_line +" 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 that contains subject line (excludes calendar invites) #Loops Through Outlook Emails in Folder for email in folder_emails: if (email.MessageClass =='IPM.Note') & ((subject_line.lower()) in (str(email.Subject.lower()))): last_email = email break ## 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 that contains 'ENTER_SUBJECT_LINE_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 – getting the last email by subject line. 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!
Leave a Reply