smtp server simulation using Python

Dave Borne dborne at gmail.com
Mon Jun 18 11:35:00 EDT 2007


> I have a (web) development computer w/o an SMTP server and want to test
> form generated e-mail using a dummy SMTP server that delivers the mail
> message to a file, or better yet, to a text editor instead of actually
> sending it.

Here's a quick and dirty script I use this for email testing purposes
- it's windows specific, but that's easy enough to change.

import smtpd, os, time, asyncore

class mailserver(smtpd.SMTPServer):
    def __init__(self):
        smtpd.SMTPServer.__init__(self, ('',25), None)
        print 'Mailsink listening on port 25'

    def process_message(self, peer, mailfrom, rcpttos, data):
        basepath='c:\\.maildump'

        print 'mail from: %s to: %s' %(mailfrom, repr(rcpttos))
        for rcpt in rcpttos:
            rcpt = rcpt.split('@')[0]
            try:
                os.mkdir(basepath+'\\'+rcpt)
            except OSError:
                pass

            f =
file(basepath+'\\'+rcpt+'\\'+mailfrom+time.strftime('%Y%m%d%H%M%S'),
'w')
            f.write(data)
            f.close()

def loop ():
    x = mailserver()
    try:
        asyncore.loop(timeout=2)
    except KeyboardInterrupt:
        print'interrupt'
        x.close()

if __name__=='__main__':
    loop()



More information about the Python-list mailing list