The*_*min 0 python email parsing imap plaintext
我一直在努力,并且错过了标记.
我能够通过imaplib连接并获取邮件.
msrv = imaplib.IMAP4(server)
msrv.login(username,password)
# Get mail
msrv.select()
#msrv.search(None, 'ALL')
typ, data = msrv.search(None, 'ALL')
# iterate through messages
for num in data[0].split():
typ, msg_itm = msrv.fetch(num, '(RFC822)')
print msg_itm
print num
Run Code Online (Sandbox Code Playgroud)
但我需要做的是将消息的正文作为纯文本,我认为这适用于电子邮件解析器,但我在使其工作时遇到问题.
有没有人有我可以看到的完整例子?
谢谢,
为了获得电子邮件正文的纯文本版本,我做了类似的事......
xxx= data[0][1] #puts message from list into string
xyz=email.message_from_string(xxx)# converts string to instance of message xyz is an email message so multipart and walk work on it.
#Finds the plain text version of the body of the message.
if xyz.get_content_maintype() == 'multipart': #If message is multi part we only want the text version of the body, this walks the message and gets the body.
for part in xyz.walk():
if part.get_content_type() == "text/plain":
body = part.get_payload(decode=True)
else:
continue
Run Code Online (Sandbox Code Playgroud)