payload of an email in string format, python

I got payload as a string instance using get_payload() method. But I want my payload in a way where I could access it word by word I tried several things like as_string() method, flatten() method, get_charset() method , but every time there is some problem.

I got my payload using the following code

import email
from email import *
f=open('mail.txt','r')
obj=email.parser.Parser()
fp=obj.parse(f)
payload=fp.get_payload()

Answers


Just tested your snippet with a couple of my own raw emails. Works fine...

get_payload() returns either a list or string, so you need to check that first

if isinstance(payload, list):
    for m in payload:
        print str(m).split()

else:
    print str(m).split()

Edit

Per our discussion, your issue was that you were not checking is_multipart() on the fp object, which actually is a message instance. If fp.is_multipart() == True, then get_payload() will return a list of message instances. In your case, based on your example mail message, it was NOT multipart, and fp was actually the object you were interesting in.


I got my payload as a string as my fp was not multipart If it could have been a multipart, it would have returned a list of strings so now I can just use the following code

payload=fp.get_payload()
abc=payload.split(" ")

it gives me the output as follows ['good', 'day\nhttp://72.167.116.186/image/bdfedx.php?iqunin=3D41\n\n', '', '', '', '', '', '', '', '', '', '', '', 'Sun,', '18', 'Dec', '2011', '10:53:43\n_________________\n"She', 'wiped', 'him', 'dry', 'with', 'soft', 'flannel,', 'and', 'gave', 'him', 'some', 'clean,', 'dry', 'clothes,=\n', 'and', 'made', 'him', 'very', 'comfortable', 'again."', '(c)', 'Lyrica', 'wa946758\n']

thanks to jdi :) p.s. couldnt post it as an answer yesterday, as there was some restriction with points


Need Your Help

Android Sim Change Detect, error in Log.e

android service broadcastreceiver

I have error when refer to LogCat. I have store the storedsimcard(1st) and compare with currentsimcard(2nd), if sim serial is different, logcat will print out sim changed. But i had problem with my

Indexed array quicksort debug

c++ debugging quicksort

I'm going nuts over this special quicksort algorithm and I don't know where is the problem. I've found an example on a forum that explains very well what I'm trying to do.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.