我正在尝试仅获取我在Python中通过imaplib收到的电子邮件的text/plain
部分:
#!/usr/bin/env python
import imaplib
import sys
from pprint import pprint
from email.parser import HeaderParser
from email.header import decode_header
reload(sys)
sys.setdefaultencoding("utf-8")
conn = imaplib.IMAP4_SSL('host')
conn.login('username', 'password')
#conn.select('Inbox', readonly=True)
conn.select('Inbox')
a, b = conn.search(None, '(UNSEEN)')
if b[0]:
c = b[0]
d = c.split()
e = d[-1]
#(BODY[HEADER.FIELDS (SUBJECT FROM)])
data = conn.fetch(e,'(BODY[1] BODY[HEADER.FIELDS (SUBJECT FROM)])')
#pprint(data)
body_data = data[1][0][1]
header_data = data[1][1][1]
#print(body_data)
# parser = HeaderParser()
# msg = parser.parsestr(header_data)
#
# print (decode_header(msg['From'])[0][0].decode('utf-8') + ": " + decode_header(msg['Subject'])[0][0].decode('utf-8'))
但是,如果是多部分消息,我最终将“ body_data”作为带有所有部分的消息的主体。
我尝试使用示例here:
raw_message = data[0][1]
msg = email.message_from_string(raw_message)
for part in msg.walk():
# each part is a either non-multipart, or another multipart message
# that contains further parts... Message is organized like a tree
if part.get_content_type() == 'text/plain':
print part.get_payload() # prints the raw text
但是,由于某些原因,part.get_payload()
仅从文本中返回单个字符。
我被迫使用Python 2.7.13。
通过在BODY[1]
命令中将BODY[1.1]
更改为fetch
,我可以得到想要的东西: