通过python在IMAP中自定义标记消息

问题描述:

有没有一种方法可以将IMAP文件夹中的消息用Python标记到custome标记中?像汤姆斯邮件,约翰的除草废话等等?通过python在IMAP中自定义标记消息

+0

[IMAP RFC](http://tools.ietf.org/html/rfc3501#section-2.3.2)指定客户端可以设置自己的标志,但不管服务器是否保留他们支持任意标志。 – sarnold 2011-05-27 23:45:08

+0

看看http://imapclient.freshfoo.com/。我不知道它是否满足你的需求,因为我不确定你想要做什么。它可能有些用处。 – 2011-05-27 23:54:41

Cyrus IMAP server支持用户定义的消息标志(又名标签,标签,关键字),您可以使用Alpine mail client进行标签实验。在Alpine中选择(S)etup - >(C)onfig,向下滚动到keywords部分并输入所需标志名称的列表。

要设置来自Python的消息标志,您可以使用标准的imaplib模块。下面是一个消息设置标志的例子:

import imaplib 
im = imaplib.IMAP4(hostname) 
im.login(user, password) 
im.select('INBOX') 

# you can use im.search() to obtain message ids 
msg_ids = '1, 4, 7' 
labels = ['foo', 'bar', 'baz'] 

# add the flags to the message 
im.store(msg_ids, '+FLAGS', '(%s)' % ' '.join(labels)) 

# fetch and print to verify the flags 
print im.fetch(ids, '(FLAGS)') 

im.close() 
im.logout() 

有一点要记住的是,发送到服务器的标志不包含空格。如果您发送+FLAGS (foo bar)到服务器,这将设置两个标志foobar。像Alpine这样的客户可以让你输入带空格的标志,但只会将最后一个非空格部分发送给服务器 - 它把它看作是一个唯一的标识符。如果您指定标记abc 123它将在消息上设置123,并在消息视图中显示abc