虽然我在从很多网站上搜索了很多之后做了大部分工作,但我仍然无法获得我想要的正确输出。代码:import imaplibimport smtplibimport emailmail=imaplib.IMAP4_SSL("imap.gmail.com")mail.login("**************@gmail.com","********")mail.select('inbox')type,data=mail.search(None,'ALL')mail_ids=data[0]id_list=mail_ids.split()for i in range(int(id_list[-1]),int(id_list[0])-1,-1): typ,data=mail.fetch(i,'(RFC822)') for response_part in data : if isinstance(response_part,tuple): msg=email.message_from_string(response_part[1]) email_from=msg['from'] email_subj=msg['subject'] c=msg.get_payload(0) print email_from print "subj:",email_subj print c输出:Bharath Joshi subj: hehe From nobody Tue Dec 25 15:48:52 2018 Content-Type: text/plain; 字符集=“UTF-8”你好444444444Bharath Joshi subj: From nobody Tue Dec 25 15:48:52 2018 Content-Type: text/plain; 字符集=“UTF-8”33333Bharath Joshi subj: From nobody Tue Dec 25 15:48:53 2018 Content-Type: text/plain; 字符集=“UTF-8”你好--22困扰我的是我得到的额外东西“来自任何人......”和“内容类型......”我怎样才能删除那些?
2 回答
拉莫斯之舞
TA贡献1820条经验 获得超10个赞
电子邮件的“美丽”……显然您正面临着多部分电子邮件,对于这些,该get_payload()方法还输出标题。你需要msg.walk()像这样使用:
for response_part in data :
if isinstance(response_part,tuple):
msg=email.message_from_string(response_part[1])
print "subj:", msg['subject']
print "from:", msg['from']
print "body:"
for part in msg.walk():
if part.get_content_type() == 'text/plain':
print part.get_payload()
添加回答
举报
0/150
提交
取消