我有一个python脚本,必须获取看不见的消息,处理它,并标记为看到(或读取)
登录后我这样做:
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)')
第一个问题是,搜索返回所有消息,而不仅仅是UNSEEN。 第二个问题是消息未标记为SEEN。
任何人都可以帮我一把吗?
谢谢!
答案 0 :(得分:13)
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')
答案 1 :(得分:3)
我认为标志名称需要以反斜杠开头,例如:\ SEEN
答案 2 :(得分:0)
我对imaplib不太熟悉,但我用 imapclient模块
很好地实现了这一点curl
我们想说我想浏览未读电子邮件,回复发件人并将电子邮件标记为已读。我从这里调用smtp函数来撰写并发送回复。
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.
我希望这会有所帮助