简体   繁体   English

使用python(imaplib)提取主题和发件人

[英]Extract subject and sender using python (imaplib)

I know there are some other posts out there, but i tried to use them but most of I couln´t make them run or they are simply to old. 我知道那里还有其他帖子,但是我尝试使用它们,但是我大多数都不会使它们运行,或者它们只是陈旧的。 I hope there is someone that can help me. 我希望有人可以帮助我。

I simply want to extract the subject and the sender of all new emails in my gmail account and set them von unread to read. 我只是想提取我的gmail帐户中所有新电子邮件的主题和发件人,并将它们设置为von unread以便阅读。

So far I just have the IMAP4 Example from the doc that gives me all my mails: 到目前为止,我只从文档中获取了IMAP4示例,该示例为我提供了所有邮件:

import imaplib

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('mymail@gmail.com', 'mypassword')
mail.list()
mail.select('inbox')

typ, data = mail.search(None, 'ALL')
for num in data[0].split():
   typ, data = mail.fetch(num, '(RFC822)')
   print ('Message %s\n%s\n' % (num, data[0][1]))
mail.close()

mail.logout()

So i need to add that i only want to have new mails & set them vom unread to read... 因此,我需要补充一点,就是我只想接收新邮件并将它们设置为未读以阅读...

Thanks for your help. 谢谢你的帮助。

You can study RFC 2060 , IMAP4.search() and IMAP4.store() for the action. 您可以研究RFC 2060IMAP4.search()IMAP4.store()来进行操作。

# To list the unseen mail
result, data = mail.search(None, 'UNSEEN')

# Mark message as seen
for message in data[0].split():
   mail.store(message, '+FLAGS', '\\Seen')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM