Skip to content

Instantly share code, notes, and snippets.

@poke
Last active May 15, 2018 10:07
Show Gist options
  • Save poke/6091a355db13c60153e9 to your computer and use it in GitHub Desktop.
Save poke/6091a355db13c60153e9 to your computer and use it in GitHub Desktop.
Simple debug mail server
#!/usr/bin/env python3
import argparse
import asyncore
import email.header
import email.parser
import smtpd
def decodeHeader (value):
return email.header.make_header(email.header.decode_header(value))
class DebugServer (smtpd.SMTPServer):
def process_message (self, peer, mailfrom, rcpttos, data, **kwargs):
print('-' * 60)
if isinstance(data, bytes):
data = data.decode()
message = email.parser.Parser().parsestr(data)
for k, v in message.items():
print('{}: {}'.format(k, decodeHeader(v)))
print()
print(message.get_payload(decode=True).decode())
def main ():
parser = argparse.ArgumentParser(description='Simple debug SMTP server.')
parser.add_argument('host', default='localhost', nargs='?', help='Server host')
parser.add_argument('port', default=8025, nargs='?', help='SMTP port')
args = parser.parse_args()
server = DebugServer((args.host, args.port), None)
print('Started debug SMTP server on {}:{}'.format(args.host, args.port))
asyncore.loop(timeout=2)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment