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

Generates a mailbox with lots of messages.

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
import itertools
import subprocess
import mailbox



def create_message(frm, to, content, headers = None):
    if not headers: 
        headers = {}
    m = mailbox.Message()
    m['from'] = frm
    m['to'] = to
    for h,v in headers.iteritems():
        m[h] = v
    m.set_payload(content)
    return m


def generate_content(prefix):
    return """%s
--
%s"""%(prefix, subprocess.Popen(["/usr/games/fortune"], stdout=subprocess.PIPE).stdout.read())


def main(mbox_file, number):
    mbox = mailbox.mbox(mbox_file)
    messages = []
    froms = itertools.cycle(["noufal@gmail.com", "noufal@emacsmovies.org", "noufal@nibrahim.net.in"])
    tos = itertools.cycle(["user1@example.com", "user2@example.com"])


    for n in range(int(number)):
        message = create_message(froms.next(),
                                 tos.next(),
                                 generate_content("number %d"%n),
                                 {"Subject": "Test email #%d"%n})
        mbox.add(message)

    mbox.close()
    

    

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv[1], sys.argv[2]))

When testing email clients, it's useful to have throwaway mailboxes that contain lots of email message with various senders, receivers, subjects and payloads.

This script can generate such a mailbox.

Running it like so

python create_mailbox.py sample.mbox 1000

will create a mailbox with 1000 messages. The from addresses will cycle through the "froms" and the to addresses will cycle through the "tos".

Payloads are generated using the "generate_content" function which can be modified.