Remove the "RE:" when replying to emails using Python


I am using python to automate some outlook email replies and I need to remove the "RE:" from the subject when using the email.ReplyAll(), I tried to do email.Subject = subject before replying but it still adds the "RE:".

FYI, I do not intend to remove the default functionality of Outlook to add "RE:" when replying to emails. I only intend to remove it when using the python script.

import win32com.client

num_emails_limit = 10 #Loops through last 10 emails in folder
folder_name = 'Sent Items' #Name of Outlook folder I want to search

#Opens Outlook application
outlook_application = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
outlook_mailbox = outlook_application.Folders.Item(1)

#Finds the correct Outlook Folder by searching for a folder that is equal to folder_name
for folder in outlook_mailbox.Folders:
    if folder.Name == folder_name:
        found_folder = folder

Filter = "[Subject] = 'SomeWord'"
folder_emails = found_folder.Items.Restrict(Filter)

folder_emails.Sort("[ReceivedTime]", True)

#Loops Through Outlook Emails
counter = 0
for email in folder_emails:
        email_subject = str(email.Subject)
        print('Subject: ' + email_subject)
        counter = counter +1

    #If the email I want to reply to contains "SomeWord"
    if "SomeWord" in email.Subject:
        #Change the subject to "OtherWord"
        subject = "OtherWord"
        email.Subject = subject
    if counter == num_emails_limit:

So using this code the Subject of the email will be "RE: OtherWord" instead of just "OtherWord". Is there any way to fix this?

Asked By: jks



You need to set the Subject property on the new reply email, not source one, for example:

email.Subject = subject

The ReplyAll method creates a reply to all original recipients from the original message. A MailItem object that represents the reply s returned back. That is where you need to change the Subject proerty by removing the prefix:

email.Subject = subject
reply = email.ReplyAll()
reply.Subject = subject

Also I’ve noticed that your code iterates over all items in the folder:

for email in folder_emails:

Instead, you need to use the Find/FindNext or Restrict methods of the Items class where you could deal only with items that correspond to the search criteria. Read more about these methods in the articles I wrote for the technical blog:

Answered By: Eugene Astafiev