简体   繁体   English

IMAP 获取发件人姓名和正文?

[英]IMAP get sender name and body text?

I am using this code:我正在使用此代码:

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

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?并且它有效,除了当我打印raw_email它返回一堆额外信息,我如何解析额外信息并仅获取发件人和正文文本?

Python's email package is probably a good place to start. Python 的电子邮件包可能是一个很好的起点。

import email
msg = email.message_from_string(raw_email)

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

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.这应该问你问,虽然当一封电子邮件有多个部分(附件、文本和 HTML 版本的正文等)时,事情会更复杂一些。

In that case, msg.is_multipart() will return True and msg.get_payload() will return a list instead of a string.在这种情况下, msg.is_multipart()将返回 True 并且msg.get_payload()将返回一个列表而不是一个字符串。 There's a lot more information in the email.message documentation. email.message文档中有更多信息。

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.或者,您无需解析原始 RFC822 格式的消息(如果电子邮件包含附件,消息可能非常大),您只需向 IMAP 服务器询问您想要的信息即可。 Changing your mail.fetch line to:将您的mail.fetch行更改为:

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

Would just request (and return) the From line of the email from the server.只会从服务器请求(并返回)电子邮件的From行。 Likewise setting the second parameter to "(UID BODY[TEXT])" would return the body of the email.同样,将第二个参数设置为"(UID BODY[TEXT])"将返回电子邮件的正文。 RFC2060 has a list of parameters that should be valid here. RFC2060有一个在这里应该有效的参数列表。

IMAP high level lib: https://github.com/ikvk/imap_tools (I am author) IMAP 高级库: https : //github.com/ikvk/imap_tools (我是作者)

from imap_tools import MailBox, A
with MailBox('imap.mail.com').login('test@mail.com', 'password', 'INBOX') as mailbox:
    for msg in mailbox.fetch(A(all=True)):
        sender = msg.from_
        body = msg.text or msg.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM