|
Description:
This tiny script logs on a POP3 server and allows you to view the top of the messages and eventually mark them for deletion.
Source: Text Source
import getpass, poplib, re
POPHOST = "pop.domain.com"
POPUSER = "jdoe"
POPPASS = ""
MAXLINES = 10
rx_headers = re.compile(r"^(From|To|Subject)")
try:
pop = poplib.POP3(POPHOST)
pop.user(POPUSER)
if not POPPASS:
POPPASS = getpass.getpass("Password for %s@%s:" % (POPUSER, POPHOST))
pop.pass_(POPPASS)
stat = pop.stat()
print "Logged in as %s@%s" % (POPUSER, POPHOST)
print "Status: %d message(s), %d bytes" % stat
bye = 0
count_del = 0
for n in range(stat[0]):
msgnum = n+1
response, lines, bytes = pop.top(msgnum, MAXLINES)
print "Message %d (%d bytes)" % (msgnum, bytes)
print "-" * 30
print "\n".join(filter(rx_headers.match, lines))
print "-" * 30
while 1:
k = raw_input("(d=delete, s=skip, v=view, q=quit) What?")
if k in "dD":
k = raw_input("Delete message %d? (y/n)" % msgnum)
if k in "yY":
pop.dele(msgnum)
print "Message %d marked for deletion" % msgnum
count_del += 1
break
elif k in "sS":
print "Message %d left on server" % msgnum
break
elif k in "vV":
print "-" * 30
print "\n".join(lines)
print "-" * 30
elif k in "qQ":
bye = 1
break
if bye:
print "Bye"
break
print "Deleting %d message(s) in mailbox %s@%s" % (count_del, POPUSER, POPHOST)
print "Closing POP3 session"
pop.quit()
except poplib.error_proto, detail:
print "POP3 Protocol Error:", detail
Discussion:
If you're still behind a slow internet link, sometimes you don't want to wait that this funny 10 megabytes mpeg movie (yes, the one you already received twice yesterday) is fully downloaded to read your mail... Instead of telneting your POP server, you may use this small script to inspect your mailbox and do some cleaning...
It uses Python's standard POP3 library (the poplib module) to connect to your mailbox and then it prompts you what to do for any undelivered messages. You may view the top of the message, leave it on the server or mark it for deletion.
No particular tricks or hacks are used in this piece of code, it's a simple example of poplib usage. I just hope it helps...
|