如何用Python在Outlook自动回复时移除主题中的RE:
问题:Outlook自动回复邮件时移除主题中的"RE:"标识
我用Python结合win32com.client实现Outlook邮件自动回复功能,希望调用email.ReplyAll()时,回复邮件主题里不要带"RE:"。之前尝试在回复前修改原邮件的Subject,但生成的回复主题还是会被加上"RE:"。注意我不想修改Outlook默认的回复加"RE:"的设置,只需要这个脚本里的回复去掉该标识。
原代码如下:
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: try: email_subject = str(email.Subject) print('Subject: ' + email_subject) counter = counter +1 except: pass #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 email.ReplyAll().Display() if counter == num_emails_limit: break
运行后回复主题显示为RE: OtherWord,不是预期的OtherWord,该怎么解决?
解决方案
问题出在你修改的是原邮件的主题,但ReplyAll()方法会自动基于原邮件生成新的回复邮件,并且Outlook的内置逻辑会强制给回复邮件添加"RE:"前缀,不管原邮件主题是什么。所以正确的做法是:先调用ReplyAll()获取生成的回复邮件对象,再修改这个回复邮件的主题。
修改后的核心代码部分:
if "SomeWord" in email.Subject: # 先生成回复邮件对象 reply_email = email.ReplyAll() # 直接设置回复邮件的主题为目标内容,完全覆盖默认的带RE:的主题 reply_email.Subject = "OtherWord" # 显示回复邮件 reply_email.Display()
完整修改后的代码:
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: try: email_subject = str(email.Subject) print('Subject: ' + email_subject) counter = counter +1 except: pass #If the email I want to reply to contains "SomeWord" if "SomeWord" in email.Subject: # 先生成回复邮件对象,再修改其主题 reply_email = email.ReplyAll() reply_email.Subject = "OtherWord" reply_email.Display() if counter == num_emails_limit: break
这样生成的回复邮件主题就会是你设置的OtherWord,不会带有"RE:"前缀,同时也不会影响Outlook默认的回复设置。
内容的提问来源于stack exchange,提问作者jks
相关产品推荐
相关产品推荐

