Pregunta

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()
¿Fue útil?

Solución

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.

Otros consejos

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top