smtp server simulation using Python

W

William Gill

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. Is it possible to extend the DebuggingServer class,and
override the process_message() method to accomplish this? If so, any
suggestions are appreciated.

Bill
 
J

Josiah Carlson

William said:
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. Is it possible to extend the DebuggingServer class,and
override the process_message() method to accomplish this? If so, any
suggestions are appreciated.

I would check out the smtpd module . It isn't documented, but the
source is fairly straightforward for subclassing.

- Josiah
 
D

Dave Borne

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()
 
W

William Gill

Dave said:
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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top