我正在尝试查看电子邮件的实际内容。到目前为止,我可以查看主题或任何其他标题,但我找不到一种方法来阅读正文而没有额外的东西。
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
答案 0 :(得分: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)