通过python在IMAP中自定义标记消息
答
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)
到服务器,这将设置两个标志foo
和bar
。像Alpine这样的客户可以让你输入带空格的标志,但只会将最后一个非空格部分发送给服务器 - 它把它看作是一个唯一的标识符。如果您指定标记abc 123
它将在消息上设置123
,并在消息视图中显示abc
。
[IMAP RFC](http://tools.ietf.org/html/rfc3501#section-2.3.2)指定客户端可以设置自己的标志,但不管服务器是否保留他们支持任意标志。 – sarnold 2011-05-27 23:45:08
看看http://imapclient.freshfoo.com/。我不知道它是否满足你的需求,因为我不确定你想要做什么。它可能有些用处。 – 2011-05-27 23:54:41