Python:仅检索POP3消息文本,无标题 [英] Python: Retrieving only POP3 message text, no headers

查看:77
本文介绍了Python:仅检索POP3消息文本,无标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



我现在的状态是:

  import poplib 

主机= pop.gmail.com
邮件= poplib.POP3_SSL(host)
打印mail.getwelcome()
打印mail.user( user)
打印邮件。 pass _( pass)
打印mail.stat()
打印mail.list()
打印

如果mail.stat()[1] > 0:
打印您有新邮件。
else:
打印没有新邮件。

打印

numMessages = len(mail.list()[1])$ ​​b $ b for i in range(numMessages):
for j在mail.retr(i + 1)[1]中:
打印j

mail.quit()
输入(按任意键继续。)
b

这一切都很好,除了执行 print J时,它会打印整个消息,包括标题。我只想提取主体文本而没有任何其他垃圾。



有人可以帮忙吗?谢谢!

解决方案

您可以使用电子邮件模块


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.

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.

Can anyone help? Thanks!

解决方案

You can parse eMails using the email module.

这篇关于Python:仅检索POP3消息文本,无标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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