POP and pop.py and/or poplib

Tim Roberts timr at probo.com
Thu Mar 9 07:08:27 CET 2000


"Akhar" <akhar at videotron.ca> wrote:
>
>I am trying to write a front in html for my POP server so I can do stuff
>like on Hotmail, so I tried using pop.py but when I try to use the top
>command it stalls and sits there forever! also I am not able to print a
>correct email they are missing some line or are not properly formatted
>the same thin goes for poplib.py but it doesn't have the top function. Also
>all the example on the internet do not work ! (though I didn't try the ones
>that included deletion :))

Here is an example that works.  It uses the poplib and rfc822 modules to
display a list of the headers currently in your mailbox.  It is important
to note that the poplib.top returns a tuple of three items, the second of
which is a nested tuple containing the text lines.  To view the lines, you
have to use something like  'for ln in p.top(i,15)[1]:'.

# vim: sw=4 ts=4

import os
import sys
import string
import poplib
import rfc822
import StringIO

p = poplib.POP3('your.pop.server.com')
p.user('username')
p.pass_('password')
(numMsgs, totalSize) = p.stat()
print numMsgs, "messages totalling", totalSize, "bytes."

# For each message in the mailbox...

for i in range(numMsgs):
    sio = StringIO.StringIO()
    # Read the top 50 lines, join them into a string with newlines, 
    # and pass the string to an in-memory file.
    sio.write( string.join( p.top(i+1,50)[1], '\n') )
    # Rewind...
    sio.seek( 0 )
    # ...and pass the stringfile to rfc822.
    m = rfc822.Message( sio )
    tms = m.getdate('Date')
    print '\r%3d %02d/%02d %02d:%02d' % ((i+1), tms[1], tms[2], 
        tms[3], tms[4]),
    print string.replace( m.getheader('Subject'), '\n', ' ')[:63]
    real,mail = m.getaddr('From')
    if not real: real = mail
    print '   ', real[:75]
    if (i % 15) == 14:
        s = raw_input "(press enter)"
        if s[0] == 'q': break
    else:
        print

p.quit()
--
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list