Python 如何使用uid打印单个电子邮件的主题
我希望打印一封电子邮件的主题,我已经使用Python2.7IMAP库打印了它的UID。这很简单,我是Python新手,只是在使用这个库时遇到了麻烦 以下是我所拥有的:Python 如何使用uid打印单个电子邮件的主题,python,python-2.7,email,imap,Python,Python 2.7,Email,Imap,我希望打印一封电子邮件的主题,我已经使用Python2.7IMAP库打印了它的UID。这很简单,我是Python新手,只是在使用这个库时遇到了麻烦 以下是我所拥有的: # Retrieve and store ID & UID for most recent email received result, data = mail.search(None,'ALL') # search and return sequential ids latest_id = data[0].split(
# Retrieve and store ID & UID for most recent email received
result, data = mail.search(None,'ALL') # search and return sequential ids
latest_id = data[0].split()[-1]
result, data = mail.uid('search', None, "ALL") # search and return uids instead
latest_uid = data[0].split()[-1]
现在我只想打印由latest\u uid
标识的电子邮件主题
谢谢你的帮助 获取主题的方法是使用RFC 822(电子邮件标准格式)获取整个消息数据,然后使用电子邮件模块将其转换为python电子邮件对象 使用RFC822将获取整个消息,但如果您只需要主题,则只需获取标题(注释如下)。如果您决定只获取标题,只需解析以
主题:
开头的行
imaplib文档更深入地了解了下面代码中发生的事情
import email, imaplib
# typ, msg_data = c.fetch('1', '(BODY.PEEK[HEADER])')
t, d = mail.fetch(latest_uid, '(RFC822)')
for res_part in d:
if isinstance(res_part, tuple):
msg = email.message_from_string(res_part[1])
subject = msg['subject']
令人惊叹的!工作完美。谢谢!