Reading the mail content of an mbox file using python mailbox

后端 未结 2 1244
谎友^
谎友^ 2021-01-01 17:14

I am trying to print the content of the mail ( Mail body) using Python mailbox.

import mailbox

mbox = mailbox.mbox(\'Inbox\')
i=1
for message in mbox:
             


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-01 17:58

    def getbody(message): #getting plain text 'email body'
        body = None
        if message.is_multipart():
            for part in message.walk():
                if part.is_multipart():
                    for subpart in part.walk():
                        if subpart.get_content_type() == 'text/plain':
                            body = subpart.get_payload(decode=True)
                elif part.get_content_type() == 'text/plain':
                    body = part.get_payload(decode=True)
        elif message.get_content_type() == 'text/plain':
            body = message.get_payload(decode=True)
        return body
    

    this function can give you message body if the body is plain text.

提交回复
热议问题