Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """A noddy fake smtp server."""
- from datetime import datetime
- import smtpd
- import asyncore
- import re
- class FakeSMTPServer(smtpd.SMTPServer):
- """A Fake smtp server"""
- def __init__(*args, **kwargs):
- print "Running fake smtp server on port 25"
- smtpd.SMTPServer.__init__(*args, **kwargs)
- def process_message(self, peer, mailfrom, rcpttos, data):
- tos = "_and_".join(rcpttos).replace("@", "_at_")
- ts = datetime.now().isoformat().replace(":","-")
- path = re.sub("[^A-z0-9/-]", "_", "/tmp/email/%s-%s" % (ts, tos))
- with open(path, "w") as f:
- inheaders = 1
- lines = data.split('\n')
- print >>f, "Peer : %s" % (peer,)
- print >>f, "From : %s" % (mailfrom,)
- print >>f, "To[s]: %s" % (rcpttos,)
- print >>f, '---------- MESSAGE FOLLOWS ----------'
- for line in lines:
- # headers first
- if inheaders and not line:
- print >>f, 'X-Peer:', peer[0]
- inheaders = 0
- print >>f, line
- print >>f, '------------ END MESSAGE ------------'
- if __name__ == "__main__":
- smtp_server = FakeSMTPServer(('localhost', 25), None)
- try:
- asyncore.loop()
- except KeyboardInterrupt:
- smtp_server.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement