通过python添加标记/类别以交换电子邮件
我使用的是Python3,我想为某些电子邮件添加类别 我发现这些类别出现在电子邮件的“关键字”部分下。通过这种方式,我能够确定哪些电子邮件有一个类别集或没有通过python添加标记/类别以交换电子邮件,python,email,exchange-server,Python,Email,Exchange Server,我使用的是Python3,我想为某些电子邮件添加类别 我发现这些类别出现在电子邮件的“关键字”部分下。通过这种方式,我能够确定哪些电子邮件有一个类别集或没有 import imaplib import email M = imaplib.IMAP4_SSL(host) M.login(username, password) M.select(folder) # I'd love to be able to only search for emails without keywords her
import imaplib
import email
M = imaplib.IMAP4_SSL(host)
M.login(username, password)
M.select(folder)
# I'd love to be able to only search for emails without keywords here,
# but M.search(None, '(Keywords)') doesn't work
status, data = M.search(None, 'ALL')
for num in data[0].split():
status, msg_data = M.fetch(num, '(RFC822)')
msg = email.message_from_bytes(data[0][1], email.message.EmailMessage)
# skip things which have categories already
if msg.get('Keywords') is not None:
continue
# I want to categorise the message here, e.g. add a category 'approve'
电子邮件服务器是exchange(我不确定他们使用的是什么版本)
我知道我们可以像这样在消息中添加标志
M.store(num, '+FLAGS', '//flagged')
但是将'//标记的'
更改为自定义的,如'\\approved'
会产生imaplib.error:STORE命令错误:错误[b'命令参数错误.11']
还有,我不确定这些关键词是不是标志性的东西
编辑:我正在通读(似乎是,而且关键字似乎是标志,但不要以\开头。即使如此,使用(已批准)
或已批准
也不起作用
编辑:我试图通过openssl连接到服务器。它显示
* FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
* OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
由于\*不是选项,这意味着不接受自定义标志,因此设置via+标志不起作用
编辑:我现在正在使用一种变通方法,即添加一封新的电子邮件,修改关键词,然后删除旧邮件。这感觉很难看,但我想它可以工作
非常感谢您的帮助。您可以使用
exchangelib
:
from exchangelib import Account, Credentials, DELEGATE
account = Account(
'me@example.com',
credentials=Credentials('myuser', 'topsecret'),
autodiscover=True,
access_type=DELEGATE
)
for email in account.inbox.all():
email.categories = ['foo', 'bar']
email.save()