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

This extracts all of the To addresses from a file in standard mbox format. It is used on a "Sent Items" mailbox to build an address white list. Presumably everyone you send email to is a candidate for an email white list.

Python, 24 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
#!/usr/bin/env python
"""This extracts all of the To addresses from an mbox file.
    It is used on a "Sent Items" mailbox to build an address white list.
"""
import mailbox
import sys

MAILBOXIN = sys.argv[1]

def main ():

    addr_list = []
    mb = mailbox.UnixMailbox (file(MAILBOXIN,'r'))
    for msg in mb:
        toaddr = msg.getaddr('To')[1]
        if toaddr not in addr_list:
            addr_list.append (toaddr)

    addr_list.sort()
    for addr in addr_list:
        print addr

if __name__ == '__main__':
    main ()

This extracts all of the To addresses from a file in standard mbox format. It is used on a "Sent Items" mailbox to build an address white list. Presumably everyone you send email to is a candidate for an email white list. I use this list to flag people I know as higher priority than regular mail and to automatically have their mail bypass spam filters.

This demonstrates using the mailbox module to sequence of a list of email messages. It's short and simple. I build a lot of email filters based on this pattern.