Skip to content

Instantly share code, notes, and snippets.

@ninehills
Created September 15, 2013 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ninehills/6570204 to your computer and use it in GitHub Desktop.
Save ninehills/6570204 to your computer and use it in GitHub Desktop.
A fake smtp server, use system sendmail to send the mail. (need Python > 2.4)
#!/usr/bin/env python
"""A fake smtp server, use system sendmail to send the mail. (need Python > 2.4)"""
import smtpd
import asyncore
from subprocess import Popen, PIPE
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):
try:
p=Popen(['/usr/sbin/sendmail', '-t'], stdin=PIPE)
p.communicate(data)
print "{0}({1}) sendmail success".format(mailfrom, peer)
except Exception, e:
print "{0}({1}) sendmail fail: {2}".format(mailfrom, peer, e)
if __name__ == "__main__":
smtp_server = FakeSMTPServer(('localhost', 25), None)
try:
asyncore.loop()
except KeyboardInterrupt:
smtp_server.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment