在 Python 中,查找和获取电子邮件是一个多步骤的过程,需要第三方模块
imapclient 和 pyzmail。作为概述,这里有一个完整的例子,包括登录到 IMAP 服务器,搜索电子邮件,获取它们,然后从中提取电子邮件的文本。
>>> import imapclient
>>> imapObj = imapclient.IMAPClient(‘imap.gmail.com’, ssl=True)
>>> imapObj.login(‘my_email_address@gmail.com’, ‘MY_SECRET_PASSWORD’)
‘my_email_address@gmail.com Jane Doe authenticated (Success)’
>>> imapObj.select_folder(‘INBOX’, readonly=True)
>>> UIDs = imapObj.search([‘SINCE 05-Jul-2014’])
>>> UIDs
[40032, 40033, 40034, 40035, 40036, 40037, 40038, 40039, 40040, 40041]
>>> rawMessages = imapObj.fetch([40041], [‘BODY[]’, ‘FLAGS’])
>>> import pyzmail
>>> message = pyzmail.PyzMessage.factory(rawMessages[40041][‘BODY[]’])
>>> message.get_subject()
‘Hello!’
>>> message.get_addresses(‘from’)
[(‘Edward Snowden’, ‘esnowden@nsa.gov’)]
>>> message.get_addresses(‘to’)
[(Jane Doe’, ‘jdoe@example.com’)]
>>> message.get_addresses(‘cc’)
[]
>>> message.get_addresses(‘bcc’)
[]
>>> message.text_part != None
True
>>> message.text_part.get_payload().decode(message.text_part.charset)
‘Follow the money.\r\n\r\n-Ed\r\n’
>>> message.html_part != None
True
>>> message.html_part.get_payload().decode(message.html_part.charset) ‘<div dir=”ltr”><div>So
long, and thanks for all the fish!<br><br></div>- Al<br></div>\r\n’
>>> imapObj.logout()
你不必记住这些步骤。在详细介绍每一步之后,你可以回来看这个概述,加强记忆。