Python、IMAP 和 GMail。将消息标记为 SEEN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2251977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:11:07  来源:igfitidea点击:

Python, IMAP and GMail. Mark messages as SEEN

pythonemailgmailimap

提问by Ezequiel

I have a python script that has to fetch unseen messages, process it, and mark as seen (or read)

我有一个 python 脚本,它必须获取未见的消息、处理它并标记为已见(或已读)

I do this after login in:

我在登录后这样做:

    typ, data = self.server.imap_server.search(None, '(UNSEEN)')

    for num in data[0].split():
        print "Mensage " + str(num) + " mark"
        self.server.imap_server.store(num, '+FLAGS', '(SEEN)')

The first problem is that, the search returns ALL messages, and not only the UNSEEN. The second problem is that messages are not marked as SEEN.

第一个问题是,搜索返回所有消息,而不仅仅是 UNSEEN。第二个问题是消息没有标记为 SEEN。

Can anybody give me a hand with this?

任何人都可以帮我解决这个问题吗?

Thanks!

谢谢!

回答by Avadhesh

import imaplib
obj = imaplib.IMAP4_SSL('imap.gmail.com', '993')
obj.login('user', 'password')
obj.select('Inbox')   <--- it will select inbox
typ ,data = obj.search(None,'UnSeen')
obj.store(data[0].replace(' ',','),'+FLAGS','\Seen')

回答by Devo

I think the flag names need to start with a backslash, eg: \SEEN

我认为标志名称需要以反斜杠开头,例如:\SEEN

回答by RenewAble

I am not so familiar with the imaplib but I implement this well with the imapclient module

我对 imaplib 不太熟悉,但我用imapclient 模块很好地实现了这一点

import imapclient,pyzmail,html2text
from backports import ssl
context=ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)

iobj=imapclient.IMAPClient('outlook.office365.com', ssl=True, ssl_context=context)
iobj.login(uname,pwd)# provide your username and password
iobj.select_folder('INBOX',readonly=True)# Selecting Inbox.

unread=iobj.search('UNSEEN')# Selecting Unread messages, you can add more search criteria here to suit your purpose.'FROM', 'SINCE' etc.
print('There are: ',len(unread),' UNREAD emails')

for i in unread:

    mail=iobj.fetch(i,['BODY[]'])#I'm fetching the body of the email here.
    mcontent=pyzmail.PyzMessage.factory(mail[i][b'BODY[]'])#This returns the email content in HTML format
    subject=mcontent.get_subject()# You might not need this             
    receiver_name,receiver_email=mcontent.get_address('from')
    mail_body=html2text.html2text(mcontent.html_part.get_payload().decode(mcontent.html_part.charset))# This returns the email content as text that you can easily relate with.

Let's say I want to just go through the unread emails, reply the sender and mark the email as read. I'd call the smtp function from here to compose and send a reply.

假设我只想浏览未读电子邮件,回复发件人并将电子邮件标记为已读。我会从这里调用 smtp 函数来撰写和发送回复。

import smtplib
smtpobj=smtplib.SMTP('smtp.office365.com',587)
smtpobj.starttls()
smtpobj.login(uname,pwd)# Your username and password goes here.
sub='Subject: '+str(subject)+'\n\n'# Subject of your reply
msg='Thanks for your email! You're qualified for the next round' #Some random reply :(
fullmsg=sub+new_result
smtpobj.sendmail(uname,test,fullmsg)# This sends the email. 
iobj.set_flags(i,['\Seen','\Answered'])# This marks the email as read and adds the answered flag
iobj.append('Sent Items', fullmsg)# This puts a copy of your reply in your Sent Items.

iobj.logout()
smtpobj.logout()

I hope this helps

我希望这有帮助