Python:仅检索 POP3 消息文本,无标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2218066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:05:26  来源:igfitidea点击:

Python: Retrieving only POP3 message text, no headers

pythonemailtextpop3poplib

提问by mplewis

I'm trying to make a Python program that retrieves only the body text of an email without passing headers or any other parameters. I'm not sure how to go about this.

我正在尝试制作一个 Python 程序,该程序仅检索电子邮件的正文而不传递标题或任何其他参数。我不知道该怎么做。

The goal is to be able to send basic commands to a program via message text.

目标是能够通过消息文本向程序发送基本命令。

What I have now is this:

我现在拥有的是:

import poplib

host = "pop.gmail.com"
mail = poplib.POP3_SSL(host)
print mail.getwelcome()
print mail.user("user")
print mail.pass_("pass")
print mail.stat()
print mail.list()
print ""

if mail.stat()[1] > 0:
    print "You have new mail."
else:
    print "No new mail."

print ""

numMessages = len(mail.list()[1])
for i in range(numMessages):
    for j in mail.retr(i+1)[1]:
        print j

mail.quit()
input("Press any key to continue.")

Which is all fine, except when "print J" is executed it prints the entire message, including headers. I just want to extract the body text without any additional garbage.

这一切都很好,除非执行“print J”时它会打印整个消息,包括标题。我只想提取正文而不需要任何额外的垃圾。

Can anyone help? Thanks!

任何人都可以帮忙吗?谢谢!

采纳答案by ebo

You can parse eMails using the email module.

您可以使用电子邮件模块解析电子邮件

回答by Chaos Manor

I would use emailmodule to get the body of email message with get_payload()method which skips header info.

我将使用电子邮件模块通过get_payload()方法获取电子邮件正文,方法跳过标题信息。

I added few lines to your code (they are marked with # new statementat the end of line)

我在您的代码中添加了几行(它们# new statement在行尾标记)

import poplib
import email # new statement

host = "pop.gmail.com"
mail = poplib.POP3_SSL(host)
print mail.getwelcome()
print mail.user("user")
print mail.pass_("pass")
print mail.stat()
print mail.list()
print ""

if mail.stat()[1] > 0:
    print "You have new mail."
else:
    print "No new mail."

print ""

numMessages = len(mail.list()[1])
for i in range(numMessages):
    for j in mail.retr(i+1)[1]:
        #print j
        msg = email.message_from_string(j) # new statement
        print(msg.get_payload()) # new statement

mail.quit()
input("Press any key to continue.")

回答by PaulMcG

This is a fragment of code from my own POP3 reader:

这是我自己的 POP3 阅读器的代码片段:

        response, lines, bytes = pop.retr(m)

        # remove trailing blank lines from message
        while lines[-1]=="": 
            del lines[-1]

        try:
            endOfHeader = lines.index('')
            header = lines[:endOfHeader]
            body = lines[endOfHeader+1:]
        except ValueError:
            header = lines
            body = []

This keys off the first empty line in the list of all lines as the end of the header info. Then just list slice from there to the end for the message body.

这将关闭所有行列表中的第一个空行作为标题信息的结尾。然后只需列出消息正文的从那里到末尾的切片。