使用python从发件人电子邮件地址在Outlook中创建规则
我正在尝试创建规则,将电子邮件从一长串发件人移动到特定文件夹。例如,如果我收到来自john@email.com,我希望它从“收件箱”移动到“workstuff\John”(John是workstuff的子文件夹) 我使用comtypes.clients和python来实现这一点,因为我发现了一个类似的帖子(),其中一个答案使用python中的comtypes.clients。 我也用它作为指导方针使用python从发件人电子邮件地址在Outlook中创建规则,python,email,outlook,rules,Python,Email,Outlook,Rules,我正在尝试创建规则,将电子邮件从一长串发件人移动到特定文件夹。例如,如果我收到来自john@email.com,我希望它从“收件箱”移动到“workstuff\John”(John是workstuff的子文件夹) 我使用comtypes.clients和python来实现这一点,因为我发现了一个类似的帖子(),其中一个答案使用python中的comtypes.clients。 我也用它作为指导方针 导入comtypes.client o=comtypes.client.CreateObject(
导入comtypes.client
o=comtypes.client.CreateObject(“Outlook.Application”)
rules=o.Session.DefaultStore.GetRules()
规则=规则。创建(“测试”,0)
条件=规则。条件
condition.From.Recipients.Add(str(“fabracht”))
condition.From.Recipients.ResolveAll
#.From.Recipients(“fabracht@gmail.com")
条件.Enabled=True
root_folder=o.GetNamespace('MAPI')。文件夹。项(1)
dest_folder=root_folder.Folders[“Evergreen1”]。Folders[“Chemistry”]
move=rule.Actions.MoveToFolder
移动。\移动或复制规则操作\ com\设置\已启用(True)
移动.\uuuuu移动或复制规则操作\uuuuu com\uuuuu设置\u文件夹(dest\u文件夹)
规则。保存()
我已经能够创建规则,它显示在outlook中。但规则缺少了“从”部分。基本上它说:
“在消息到达后应用此规则
将其移动到john文件夹“
我希望规则是:
“在消息到达后应用此规则
从…起john@email.com
将其移动到john文件夹“您的帖子中提到的包含以下代码,用于处理来自部分的:
'Specify the condition in a ToOrFromRuleCondition object
'Condition is if the message is from "Eugene Astafiev"
Set oFromCondition = oRule.Conditions.From
With oFromCondition
.Enabled = True
.Recipients.Add ("Eugene Astafiev")
.Recipients.ResolveAll
End With
代码应如下所示:
import comtypes.client
o = comtypes.client.CreateObject("Outlook.Application")
rules = o.Session.DefaultStore.GetRules()
rule = rules.Create("Test", 0)
condition = rule.Conditions
condition.From.Recipients.Add(str("fabracht"))
condition.From.Recipients.ResolveAll
oFromCondition = oRule.Conditions.From
oFromCondition.Enabled = True
oFromCondition.Recipients.Add("john@email.com")
oFromCondition.Recipients.ResolveAll
#.From.Recipients("fabracht@gmail.com")
condition.Enabled = True
root_folder = o.GetNamespace('MAPI').Folders.Item(1)
dest_folder = root_folder.Folders["Evergreen1"].Folders["Chemistry"]
move = rule.Actions.MoveToFolder
move.__MoveOrCopyRuleAction__com__set_Enabled(True)
move.__MoveOrCopyRuleAction__com__set_Folder(dest_folder)
rules.Save()