reading the text of an email using python

so i'm working on a school project in python for which the app needs to actively detect the presence of a email (with a specific subject but no specific sender). the app also needs to be able to read the email's body.  tutorials on the internet haven't helped too much. is there a way to do this passively(without hindering the rest of the code) using imap or poplib?
kshitij sawhneyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dr. KlahnPrincipal Software EngineerCommented:
Are you expected to handle HTML emails as well as plain text?  I would have to say that if the requirement is to handle HTML emails as well as plain text, the task is overly difficult.
kshitij sawhneyAuthor Commented:
it'll be plain text..
Flabio GatesCommented:
some old imaplib code I had:
import email, imaplib

host = 'some.imap4.server'
user = 'yourname'
passwd = 'secret'

messages = []
conn = imaplib.IMAP4(host)
conn.login(user, passwd)
try: # select the inbox
        ret, ids =, '(FROM "Somebody")')
        s = ids[0].strip()
        if s:
            ids = s.split()
            ids = []
        for id_ in ids:
            r, data = conn.fetch(id_, '(RFC822)')
            m = email.message_from_string(data[0][1])
            # m.get_payload() gives you the E-mail body
            messages.append((int(id_), m))

    finally: conn.close()
finally: conn.logout()

Open in new window

imaplib docs here.

some old poplib code I had:
import poplib, email

host = 'some.pop3.server'
user = 'yourname'
passwd = 'secret'

messages = []
conn = poplib.POP3_SSL(host)
    msgcount = conn.stat()[0]

    for i in range(1, msgcount+1):
        m = email.message_from_string('\n'.join(conn.retr(i)[1]))
        # m.get_payload() gives you the E-mail body
        messages.append((i, m))

finally: conn.quit()

Open in new window

poplib docs here.

I should warn you that the code is working on python 2.5 (I said it was old.)
The docs I linked to are for python 3.x.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Flabio GatesCommented:
Question inactive
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.