I have a MIME Message stored in a 'text/plain' file that looks like this:
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!