字符串格式的电子邮件的有效载荷,python [英] payload of an email in string format, python
问题描述
我使用 get_payload()
方法将字符串实例作为字符串实例。但我想要我的有效载荷,我可以通过单词
访问它的方式我尝试过几个东西,如 as_string()方法,flatten()方法,get_charset()方法
,但每次都有一些问题。
我使用以下代码获取了我的有效载荷
从电子邮件导入导入电子邮件
f = open('mail.txt','r')
obj = email.parser.Parser()
fp = obj.parse(f)
payload = fp.get_payload()
只需用自己的几封电子邮件测试您的代码段。工作正常...
get_payload()返回一个列表或字符串,所以你需要检查第一个
<
$ b :
print str(m).split()
修改
根据我们的讨论,您的问题是您没有在fp对象上检查is_multipart(),而实际上它是一个消息实例。如果fp.is_multipart()== True,那么get_payload()将返回一个消息实例列表。在您的情况下,根据您的示例邮件消息,它不是multipart,而是fp实际上是您感兴趣的对象。
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()
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.
这篇关于字符串格式的电子邮件的有效载荷,python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!