2

我正在尝试查看电子邮件的实际正文。到目前为止,我可以查看主题或任何其他标题,但我无法找到一种方法来阅读没有额外内容的正文。

from imapclient import IMAPClient
import email

host = 'imap.gmail.com'
user = 'myEmailAddress'
password = 'myPassword'
ssl = True

server = IMAPClient(host, use_uid=True, ssl=ssl)
server.login(user, password)

inboxInfo = server.select_folder('INBOX')
messages = server.search(['NOT DELETED'])
response = server.fetch(messages, ['RFC822', 'BODY[TEXT]'])

for msgid, data in response.iteritems():
        parsedEmail = email.message_from_string(data['RFC822'])
        body = email.message_from_string(data['BODY[TEXT]'])
        parsedBody = parsedEmail.get_payload(0)
        print parsedBody

server.logout()

并返回:

>From nobody Fri Jun  6 01:29:34 2014
>Content-Type: text/plain; charset=UTF-8
>
>Email body

我只想返回:

>Email body
4

1 回答 1

0

get_payload(i)返回一个Message对象。

您可以使用该Message.walk()方法并测试text/plain

例如

for part in message.walk():
   if part.get_content_type() == "text/plain":
      do_stuff_with_text_parts(part)
于 2014-06-06T07:11:09.033 回答