Created
September 15, 2013 12:06
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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