Fetch an email with imaplib but do not mark it as SEEN

让人想犯罪 __ 提交于 2019-12-17 06:10:58

问题


I want to parse some emails from a user 's inbox but when I do:

typ, msg_data = imap_conn.fetch(uid, '(RFC822)')

It marks the email as SEEN or read. This is not the desired functionality. Do you know how can I keep the email at its previous stare either SEEN or NOT SEEN?


回答1:


You might also set read_only to true when selecting the folder:

imap_conn.select('Inbox', readonly=True)



回答2:


The following should work:

typ, msg_data = imap_conn.fetch(uid, '(BODY.PEEK[HEADER])')

or BODY.PEEK[TEXT], etc.




回答3:


You can use (RFC822.PEEK) as the "message-parts" argument, according to RFC 1730 (I have not verified which servers actually implement that correctly, but it doesn't seem hard for them to).




回答4:


You may use imap_tools package: https://pypi.org/project/imap-tools/

from imap_tools import MailBox, Q

# get list of email subjects from INBOX folder
with MailBox('imap.mail.com').login('test@mail.com', 'password') as mailbox:
    # mark_seen=False - not mark emails as seen on fetch
    subjects = [msg.subject for msg in mailbox.fetch(mark_seen=False)]


来源:https://stackoverflow.com/questions/3283460/fetch-an-email-with-imaplib-but-do-not-mark-it-as-seen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!