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

I get a lot of spam. :( Unfortunately, much of this spam is not the good old 4-5k message trying to sell something...many contain attachments in the 80-150k size range. Add to this the facts that I still live off a dialup line and my mail client (the otherwise amazing sylpheed) does not filter at the POP3 level and it's clear that I need to do something about it. Hence this python applet. It uses poplib to connect to a POP3 server, list messages greater than a given size (default is 50k), and then prompt for which messages to delete. It's been pretty handy for me.

Python, 78 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
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
#!/usr/local/bin/python
# 2002-05-19 - fsf
# last modified: 2002-06-16

import sys, re, string
import poplib
from poplib import *

pop_host = 'insert_pop3_server_here'
pop_user = 'insert_username_here'
pop_pass = 'insert_password_here'

filter_size = 50000
if len (sys.argv) > 1:
    filter_size = sys.argv[1]

p = POP3 (pop_host)
p.user (pop_user)
p.pass_ (pop_pass)

[msgs, psize] = p.stat ()
if msgs < 1:
    print 'No messages found in mailbox ... exiting!'
    sys.exit (0)
else:
    print msgs, 'found with a total size of', psize, 'octets...'

re_from = re.compile ("^From:")
re_to = re.compile ("^To:")
re_subject = re.compile ("^Subject:")

filtered = []

for pid in range (1, msgs + 1):
    h_from = h_to = h_subject = ''

    list_fields = string.split (p.list (pid))
    size = list_fields[2]
    if int (size) < int (filter_size):
        continue

    filtered.append (pid)

    top = p.top (pid, 0)
    headers = top[1]
    for line in headers:
        if re_from.match (line):
            h_from = line
        elif re_to.match (line):
            h_to = line
        elif re_subject.match (line):
            h_subject = line

    print 'Message %d (%s octets)' % (pid, size)
    print h_from
    print h_to
    print h_subject
    print

input = raw_input ("Delete Messages [1 - %d, (A)ll Filtered, (Q)uit]: " \
    % (msgs))

input = string.lower (input)

if (input == 'all') or (input == 'a'):
    print 'Deleting all messages larger than %s bytes...' % (filter_size)
    delete = filtered
    # delete = range (1, msgs + 1)
elif (input == 'q'):
    sys.exit (0)
else:
    delete = string.split (input)

for pid in delete:
    print 'Deleting message %s...' % pid
    p.dele (pid)

p.quit ()

A user without high-speed internet access who gets spam with large attachments might want to take a look if his/her email client cannot filter at the POP3 server level.

I do plan on working on the sylpheed code to add a way to pop up a confirmation box as large emails are attempted to be downloaded, but this will work for now.