A how to use POP3 question

Steve Holden sholden at holdenweb.com
Mon Feb 24 19:57:03 EST 2003


"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"
try:
    p = poplib.POP3(SRVR)
except:
    print "Unable to contact server %s" % (SRVR, )
    sys.exit(-1)

try:
    print p.user(USER)
    print p.pass_(PASS)
except:
    print "Authentication failure"
    sys.exit(-2)

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 "-------------------------------"
p.quit()
sys.exit()

regards
--
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