#!/usr/bin/env python # # fakesmtp - A fake SMTP server # # Copyright (c) 2007, David Cooper # All rights reserved. # # Dedicated to Kate Lacey # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice, the above dedication, and this permission notice appear # in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # import asyncore import errno import getopt import smtpd import socket import sys import time USAGE = ''' fakesmtp - A fake SMTP server USAGE: fakemail [-p port] [-h hostname] [-q] fakemail --help ARGUMENTS: -p port The port to bind to (default: 25) -h hostname The hostname to bind to (default: localhost) -q Enable quiet mode --help This help message ''' quiet = False host = 'localhost' port = 25 class FakeSMTPServer(smtpd.SMTPServer): def __init__(self, host, port): smtpd.SMTPServer.__init__(self, (host, port), None) def process_message(self, peer, mailfrom, rcpttos, data): if not quiet: t = time.strftime("%m/%d/%Y %I:%M:%S %p") print "--------------- %s ---------------" % t print data if __name__ == '__main__': try: opts, args = getopt.getopt(sys.argv[1:], "p:h:q") except Exception, getopt.GetoptError: print USAGE sys.exit(0) for opt, value in opts: if opt == '-p': port = int(value) elif opt == '-h': host = value elif opt == '-q': quiet = True try: stmp = FakeSMTPServer(host, port) except socket.error, (error_number, error_desc): if error_number != errno.EACCES: raise print "Permission denied when binding on '%s:%s'." % (host, port) sys.exit(1) if not quiet: print "Listening on '%s:%s'." % (host, port) try: asyncore.loop() except KeyboardInterrupt: if not quiet: print "Exiting."