Advertisement
Kagee

Untitled

Jan 26th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """A noddy fake smtp server."""
  3.  
  4. from datetime import datetime
  5. import smtpd
  6. import asyncore
  7.  
  8. class FakeSMTPServer(smtpd.SMTPServer):
  9.     """A Fake smtp server"""
  10.  
  11.     def __init__(*args, **kwargs):
  12.         print "Running fake smtp server on port 25"
  13.         smtpd.SMTPServer.__init__(*args, **kwargs)
  14.  
  15.     def process_message(self, peer, mailfrom, rcpttos, data):
  16.         tos = "_and_".join(rcpttos).replace("@", "_at_")
  17.         ts = datetime.now().isoformat().replace(":","-")
  18.         path = "/tmp/email/%s-%s" % (ts, tos)
  19.         with open(path) as f:
  20.             inheaders = 1
  21.             lines = data.split('\n')
  22.             print "Peer: %s" % (peer,)
  23.             print "From: : %s" % (mailfrom,)
  24.             print "RCPTTOS: %s" % (rcpttos,)
  25.             print '---------- MESSAGE FOLLOWS ----------'
  26.             for line in lines:
  27.                 # headers first
  28.                 if inheaders and not line:
  29.                     print 'X-Peer:', peer[0]
  30.                     inheaders = 0
  31.                 print line
  32.             print '------------ END MESSAGE ------------'
  33.  
  34. if __name__ == "__main__":
  35.     smtp_server = FakeSMTPServer(('localhost', 25), None)
  36.     try:
  37.         asyncore.loop()
  38.     except KeyboardInterrupt:
  39.         smtp_server.close()
  40. ~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement