如何获取电子邮件,让他们在Python中读/未读?

时间:2015-12-08 04:52:16

标签: python email python-3.x imap

我有一个小工具,因为我的电子邮件客户端不支持gmail实现的IMAP推送扩展。典型用法是这样的:

live-email -n 10

显示前10个的id和主题。

因此,一些示例输出可能是:

1242: Hello!
1241: Your email
1240: Re: 
...

代码(此问题的简化)如下:

#!/usr/bin/env python3

from imaplib import IMAP4_SSL
from netrc import netrc
from email import message_from_bytes
from argparse import ArgumentParser

parser = ArgumentParser(description='Check email')
parser.add_argument('-n', '--count', dest='count', action='store',
                   default=1, type=int,
                   help='How many messages to show')

args = parser.parse_args()

conn = IMAP4_SSL('imap.gmail.com')
auth = netrc().hosts['imap.gmail.com']

conn.login(auth[0], auth[2])
conn.select()

typ, data = conn.search(None, 'ALL')
i = 0
for num in reversed(data[0].split()):
    i += 1
    typ, data = conn.fetch(num, '(RFC822)')
    email = message_from_bytes(data[0][1])
    print("%i: %s" % (int(num), email.get('subject')))
    if i == args.count:
        break

conn.close()
conn.logout()

以上作品。问题在于获取每封电子邮件的行为将其标记为已读。我想单独留下电子邮件的状态。如果我对IMAP有更多了解,我怀疑我知道如何做到这一点,但我不知道该协议。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

不出所料,阅读RFC 2060给了我答案。解决方案是使用(BODY.PEEK[])而不是(RFC822)。所以完整的答案是:

#!/usr/bin/env python3

from imaplib import IMAP4_SSL
from netrc import netrc
from email import message_from_bytes
from argparse import ArgumentParser

parser = ArgumentParser(description='Check email')
parser.add_argument('-n', '--count', dest='count', action='store',
                   default=1, type=int,
                   help='How many messages to show')

args = parser.parse_args()

conn = IMAP4_SSL('imap.gmail.com')
auth = netrc().hosts['imap.gmail.com']

conn.login(auth[0], auth[2])
conn.select()

typ, data = conn.search(None, 'ALL')
i = 0
for num in reversed(data[0].split()):
    i += 1
    typ, data = conn.fetch(num, '(BODY.PEEK[])')
    email = message_from_bytes(data[0][1])
    print("%i: %s" % (int(num), email.get('subject')))
    if i == args.count:
        break

conn.close()
conn.logout()