Search code examples
pythonemailtextpop3poplib

Python: Retrieving only POP3 message text, no headers


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!


Solution

  • You can parse eMails using the email module.