A how to use POP3 question, works fine now

Raaijmakers, Vincent (IndSys, GE Interlogix) Vincent.Raaijmakers at ge.com
Tue Feb 25 18:08:57 CET 2003


Forget the previous question. Delete works fine now. 
Forgot to look in poplib.


-----Original Message-----
From: Steve Holden [mailto:sholden at holdenweb.com]
Sent: Monday, February 24, 2003 7:57 PM
To: python-list at python.org
Subject: Re: A how to use POP3 question

"Raaijmakers, Vincent (IndSys, GE Interlogix)" <Vincent.Raaijmakers at ge.com>
wrote in message news:mailman.1046128449.11314.python-list at python.org...
> After reading, coding, trial and error work, I have the
> feeling I'm about to create a munster of a script instead
> of something nice and clean...
> I would like to get a list of subjects of all my emails
> on my POP3 server.
> What is the easiest way to do that?
> Something like:
>         mailbox = poplib.POP3(host) mailbox.user(userName)
>         mailbox.pass_(password)
>         return mailbox.<give me the list with subjects>
> Thanks for any help.

Here's an example from chapter 5 of "Python Web Programming". It might do
about what you want, or possibly a bit more:

import poplib
import rfc822, sys, StringIO

SRVR = "mymailserver.com"
USER = "user"
PASS = "password"
    p = poplib.POP3(SRVR)
    print "Unable to contact server %s" % (SRVR, )

    print p.user(USER)
    print p.pass_(PASS)
    print "Authentication failure"

msglst = p.list()[1]
for m in msglst:
    mno, size = m.split()
    lines = p.retr(mno)[1]
    print "----- Message %s --------------" % (mno, )
    file = StringIO.StringIO("\r\n".join(lines))
    msg = rfc822.Message(file)
    body = file.readlines()
    addrs = msg.getaddrlist("to")
    print "%-15s %s" % ("Recipient", "Email Address")
    for rcpt, addr in addrs:
        print "%-15s %s" % (rcpt, addr)
    print len(body), "lines in message body"
print "-------------------------------"

Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Register for PyCon now!            http://www.python.org/pycon/reg.html


More information about the Python-list mailing list