[英]Python - Moving Outlook Mail from Inbox to Folder Based on Folder Name
I have a table of clients, their emails, and a representative that they are assigned to:我有一张客户表、他们的电子邮件以及他们分配给的代表:
Client![]() |
Email ![]() |
Rep![]() |
---|---|---|
1234 ![]() |
bob@bobsco.com ![]() |
John![]() |
5678 ![]() |
jim@jimsco.com![]() |
Jane![]() |
The emails I receive from these clients go into an inbox that has subfolders with each rep's name.我从这些客户 go 收到的电子邮件发送到一个收件箱中,该收件箱的子文件夹中包含每个代表的姓名。
I receive hundreds of these emails, and would like to sort them automatically.我收到了数百封这样的电子邮件,并希望自动对它们进行分类。
How would I write a loop that would assign these emails to the correct rep's folder based on the email address that sent it?我将如何编写一个循环,根据发送邮件的 email 地址将这些电子邮件分配到正确的代表文件夹? Alternatively, the email that gets sent out will always have the client number in the body, I could loop over that as well.
或者,发出的 email 将始终在正文中包含客户端编号,我也可以循环处理它。
import win32com.client as client
# create outlook instance
outlook = client.Dispatch('Outlook.Application')
# get the namespace object
namespace = outlook.GetNameSpace("MAPI")
inbox = namespace.GetDefaultFolder(6)
To process incoming emails you need to handle the NewMailEx
event of the Application
class. This event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem
, MeetingItem
, or SharingItem
.要处理传入的电子邮件,您需要处理
Application
class 的NewMailEx
事件。对于 Microsoft Outlook 处理的每个收到的项目,此事件都会触发一次。该项目可以是几种不同项目类型之一,例如MailItem
、 MeetingItem
或SharingItem
。 The EntryIDsCollection
string contains the Entry ID that corresponds to that item. EntryIDsCollection
字符串包含对应于该项目的条目 ID。 Use the Entry ID returned in the EntryIDCollection
strign to call the NameSpace.GetItemFromID method and process the item.使用
EntryIDCollection
字符串中返回的条目 ID 调用NameSpace.GetItemFromID方法并处理该项目。 Use this method with caution to minimize the impact on Outlook performance.请谨慎使用此方法,以尽量减少对 Outlook 性能的影响。
Also you may consider handling the ItemAdd
event of the Items
class (on the folder) which is fired when items are added to the folder.您也可以考虑处理
Items
class(在文件夹上)的ItemAdd
事件,该事件在将项目添加到文件夹时触发。
You can read more about possible solutions and their pros and cons in the series of articles:您可以在系列文章中阅读更多关于可能的解决方案及其优缺点的信息:
Alternatively, the email that gets sent out will always have the client number in the body, I could loop over that as well.
或者,发出的 email 将始终在正文中包含客户端编号,我也可以循环处理它。
In that case you need to handle the ItemSend
event where you could set the MailItem.SaveSentMessageFolder property to put sent items to the folder specified.在这种情况下,您需要处理
ItemSend
事件,您可以在其中设置MailItem.SaveSentMessageFolder属性以将已发送的项目放入指定的文件夹。 For example, the following VBA macro shows how to use the property in the code (the Outlook object model is common for all programming languages):例如,下面的 VBA 宏显示了如何在代码中使用该属性(Outlook object model 对所有编程语言都是通用的):
Sub SetSentFolder()
Dim myItem As Outlook.MailItem
Dim myResponse As Outlook.MailItem
Dim mpfInbox As Outlook.Folder
Dim mpf As Outlook.Folder
Set mpfInbox = Application.Session.GetDefaultFolder(olFolderInbox)
Set mpf = mpfInbox.Folders.Add("SaveToMySentItemsFolder")
Set myItem = Application.ActiveInspector.CurrentItem
Set myResponse = myItem.Reply
myResponse.Display
myResponse.To = "Eugene"
Set myResponse.SaveSentMessageFolder = mpf
myResponse.Send
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.