Last active
May 15, 2018 10:07
-
-
Save poke/6091a355db13c60153e9 to your computer and use it in GitHub Desktop.
Simple debug mail server
This file contains 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 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