Welcome, guest | Sign In | My Account | Store | Cart

A simple script that shows the headers from messages in a popbox and allows the user to delete messages on the server before downloading them. Only works with pop-servers that support the TOP-command.

Python, 46 lines
 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
#!/usr/bin/python

import sys
import getpass
import poplib

def popPeek(server, user, port=110):

    try:
        P = poplib.POP3(server, port)
        P.user(user)
        P.pass_(getpass.getpass())
    except:
        print "Failed to connect to server."
        sys.exit(1)

    deleted = 0

    try:
        l = P.list()
        msgcount = len(l[1])
        for i in range(msgcount):
            msg = i+1
            top = P.top(msg, 0)
            for line in top[1]:
                print line
            input = raw_input("D to delete, any other key to leave message on server: ")
            if input=="D":
                P.dele(msg)
                deleted += 1
        P.quit()                
        print "%d messages deleted. %d messages left on server" % (deleted, msgcount-deleted)
    except:
        P.rset()
        P.quit()
        deleted = 0
        print "\n%d messages deleted. %d messages left on server" % (deleted, msgcount-deleted)

if __name__ == "__main__":

    if len(sys.argv)<3:
        print """
Usage: popPeek.py server username [port]
        """
    else:
        popPeek(sys.argv[1], sys.argv[2], sys.argv[3])

I wrote this because one of the accounts I (have to) use is flooded with the myDoom-virus and spambayes doesn't (yet) seem able to catch enough of them for me to be able to ignore it.

1 comment

Steven Kryskalla 20 years, 2 months ago  # | flag

Nice script. The only suggestion I would make would be to change

input = raw_input("D to delete, any other key to leave message on server: ")
if input=="D":

to be case insensitive (input.upper() or raw_input("...").upper()), the first time I ran the script I used all lowercase d's and was suprised to see 0 messages were deleted.

Also, the "D to delete, any other key to leave message on server:" doesn't wait for any key to be pressed, it only works after you press enter, you'd have to use a getch() type function to do that (see recipe #134892 or search for "getch")