gpt4 book ai didi

python - 使用 python 从发件人电子邮件地址在 Outlook 中创建规则

转载 作者:行者123 更新时间:2023-12-01 07:36:13 27 4
gpt4 key购买 nike

我正在尝试创建规则以将电子邮件从一长串发件人列表移动到特定文件夹。例如,如果我收到一封来自 john@email.com 的电子邮件,我希望将其从“收件箱”移动到“workstuff\John”(john 是workstuff 的子文件夹)。

我使用 comtypes.clients 和 python 来执行此操作,因为我发现了类似的帖子 ( Setting a property using win32com ),其中答案之一在 python 中使用 comtypes.clients 。我也在使用https://learn.microsoft.com/en-us/office/vba/outlook/how-to/rules/create-a-rule-to-move-specific-e-mails-to-a-folder作为指导方针。

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

#.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()

我已经能够创建规则,该规则显示在 Outlook 中。但该规则缺少“来自”部分。基本上它说:

"消息到达后应用此规则将其移至 john 文件夹“

我预计规则是:

"消息到达后应用此规则来自 john@email.com将其移至 john 文件夹“

最佳答案

article您的帖子中提到的包含以下用于处理 From 部分的代码:

'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()

关于python - 使用 python 从发件人电子邮件地址在 Outlook 中创建规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56980384/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com