This tiny script logs on a POP3 server and allows you to view the top of the messages and eventually mark them for deletion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | #!/usr/local/bin/python
#
# This script is a helper to clean POP3 mailboxes
# containing malformed mails that hangs MUA's, that
# are too large, or whatever...
#
# It iterates over the non-retrieved mails, prints
# selected elements from the headers and prompt the
# user to delete bogus messages.
#
# Written by Xavier Defrang <xavier.defrang@brutele.be>
#
#
import getpass, poplib, re
# Change this to your needs
POPHOST = "pop.domain.com"
POPUSER = "jdoe"
POPPASS = ""
# How many lines of message body to retrieve
MAXLINES = 10
# Headers we're actually interrested in
rx_headers = re.compile(r"^(From|To|Subject)")
try:
# Connect to the POPer and identify user
pop = poplib.POP3(POPHOST)
pop.user(POPUSER)
if not POPPASS:
# If no password was supplied, ask for it
POPPASS = getpass.getpass("Password for %s@%s:" % (POPUSER, POPHOST))
# Authenticate user
pop.pass_(POPPASS)
# Get some general informations (msg_count, box_size)
stat = pop.stat()
# Print some useless information
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
# Retrieve headers
response, lines, bytes = pop.top(msgnum, MAXLINES)
# Print message info and headers we're interrested in
print "Message %d (%d bytes)" % (msgnum, bytes)
print "-" * 30
print "\n".join(filter(rx_headers.match, lines))
print "-" * 30
# Input loop
while 1:
k = raw_input("(d=delete, s=skip, v=view, q=quit) What?")
if k in "dD":
# Mark message for deletion
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
# Time to say goodbye?
if bye:
print "Bye"
break
# Summary
print "Deleting %d message(s) in mailbox %s@%s" % (count_del, POPUSER, POPHOST)
# Commit operations and disconnect from server
print "Closing POP3 session"
pop.quit()
except poplib.error_proto, detail:
# Fancy error handling
print "POP3 Protocol Error:", detail
|
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...