smtp server simulation using Python

William Gill noreply at gcgroup.net
Sun Jun 24 13:47:17 EDT 2007



Dave Borne wrote:
>> 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.

Actually XP is where I need it.
Thanks

Bill
> 
> 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