使用imaplib获取电子邮件主题和发件人? [英] Get email subject and sender using imaplib?

查看:486
本文介绍了使用imaplib获取电子邮件主题和发件人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行响应下面显示的代码后,我得到以下响应.如何解析此响应以获取发件人(约翰·史密斯)和主题(测试)?

I am getting the following response after executing the code shown below the response. How can I parse through this response to get the sender (John Smith) and the subject (test)?

[('13010 (BODY[HEADER.FIELDS (SUBJECT FROM)] {57}', 'From: John Smith <jsmith@gmail.com>\r\nSubject: test\r\n\r\n'), ')']

-

conn.fetch(message, '(BODY[HEADER.FIELDS (SUBJECT FROM)])')

推荐答案

也许是问题/答案

Perhaps the question/answer here will help. Try something like this:

from email.parser import HeaderParser
data = conn.fetch(message, '(BODY[HEADER.FIELDS (SUBJECT FROM)])')
header_data = data[1][0][1]
parser = HeaderParser()
msg = parser.parsestr(header_data)

,然后msg应该是字典.

这篇关于使用imaplib获取电子邮件主题和发件人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆