Oscar
Oscar

Reputation: 211

Read MIME Message stored in text file with Python

I have a MIME Message stored in a 'text/plain' file that looks like this:

http://pastebin.com/sPWWC9LL

The MIME Message is supposed to be a multipart message.

How do I parse this in Python? I have tried email.message_from_string(), but it's still encoded as 'text/plain', and thus I can't use the email library to parse it.

My code looks like this:

f = open(settings.MEDIA_ROOT + '/raw.txt', 'r')
msg = email.message_from_string(f.read())
i = 1

for part in msg.walk():
    if part.get_content_maintype() == 'multipart':
        continue

    ext = mimetypes.guess_extension(part.get_content_type())
    filename = 'part-%03d%s' % (i, ext)

    fp = open(settings.MEDIA_ROOT + '/' + filename, 'wb')
    fp.write(part.get_payload(decode=True))
    fp.close()
    i += 1

I would be very grateful for any help!

Upvotes: 0

Views: 7032

Answers (1)

unutbu
unutbu

Reputation: 879799

You should be able to parse the message and walk through the parts using code from this SO answer, however, first you'll need to add

Content-type: multipart/alternative;  
    boundary="cbsms-main-boundary"

to the beginning of the message, or preserve the full original message.


I believe your code works. After adding the two lines above to MIME_Message_in_text_file.txt, I ran

test.py:

import os
import sys
import mimetypes
import email

msg = email.message_from_file(open(sys.argv[1]))

for i,part in enumerate(msg.walk(),1):
    if part.get_content_maintype() == 'multipart':
        continue
    ext = mimetypes.guess_extension(part.get_content_type())
    filename='part-%03d%s'%(i, ext)
    # filename=os.path.join('settings.MEDIA_ROOT', filename)
    print(filename)
    with open(filename, 'wb') as fp:
        fp.write(part.get_payload(decode=True))

this way:

% test.py MIME_Message_in_text_file.txt

and received:

part-002.xml
part-004.jpe
part-005.ksh

Upvotes: 5

Related Questions