IMAP获取发件人名称和正文文本

IMAP获取发件人名称和正文文本

本文介绍了IMAP获取发件人名称和正文文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码:

import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login(myusername, mypassword)
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.

result, data = mail.search(None, "ALL")

ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[-1] # get the latest

result, data = mail.fetch(latest_email_id, "(RFC822)") # fetch the email body (RFC822) for the given ID

raw_email = data[0][1] # here's the body, which is raw text of the whole email
# including headers and alternate payloads

print raw_email

作品,除了当我打印 raw_email 它返回一堆额外的信息,我如何解析,说出额外的信息,只得到从和身体文本?

and it works, except, when I print raw_email it returns a bunch of extra information, how can I, parse, per say, the extra information and get just the From and body text?

推荐答案

Python的包可能是开始的好地方。

Python's email package is probably a good place to start.

import email
msg = email.message_from_string(raw_email)

print msg['From']
print msg.get_payload(decode=True)

应该问你问虽然电子邮件具有多个部分(附件,文本和HTML版本的正文等),但事情有点复杂。

That should do ask you ask, though when an email has multiple parts (attachments, text and HTML versions of the body, etc.) things are a bit more complicated.

在这种情况下, msg.is_multipart()将返回True, msg.get_payload()将返回一个列表而不是一个字符串。 文档中有更多信息。

In that case, msg.is_multipart() will return True and msg.get_payload() will return a list instead of a string. There's a lot more information in the email.message documentation.

或者,不是解析原始的RFC822格式的消息 - 这可能非常大,如果电子邮件包含附件,您可以向IMAP服务器询问您想要的信息。将 mail.fetch 行更改为:

Alternately, rather than parsing the raw RFC822-formatted message - which could be very large, if the email contains attachments - you could just ask the IMAP server for the information you want. Changing your mail.fetch line to:

mail.fetch(latest_email_id, "(BODY[HEADER.FIELDS (FROM)])")

只会请求(并返回)来自服务器的电子邮件的行。同样将第二个参数设置为(UID BODY [TEXT])将返回电子邮件正文。 有一列在这里应该有效的参数。

Would just request (and return) the From line of the email from the server. Likewise setting the second parameter to "(UID BODY[TEXT])" would return the body of the email. RFC2060 has a list of parameters that should be valid here.

这篇关于IMAP获取发件人名称和正文文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:57