Skip to content

Instantly share code, notes, and snippets.

@ptaylor
Last active March 1, 2022 10:17
Show Gist options
  • Save ptaylor/01584c37ec1c72b11964b0bacff779ff to your computer and use it in GitHub Desktop.
Save ptaylor/01584c37ec1c72b11964b0bacff779ff to your computer and use it in GitHub Desktop.
SMTP server that writes emails to the console and appends to a log file.
#!/bin/sh
cat << __EOF__ | sudo /usr/bin/env python3
from smtpd import SMTPServer
import asyncore
import datetime
PORT=25
HOST='localhost'
class LoggingServer(SMTPServer):
logName = "mail.log"
log = None
def __init__(self):
global HOST
SMTPServer.__init__(self, (HOST, 25), None)
self.log = open(self.logName, "a")
print(f"[appending to '{self.logName}']")
def process_message(self, peer, mailfrom, rcpttos, data):
today = datetime.date.today()
print("-------------------------------------------------------------------------------")
print(f"Date: {today}")
print("")
print(str(data))
self.log.write("-------------------------------------------------------------------------------\n")
self.log.write(f"Date: (today)")
self.log.write("\n")
self.log.write("\n")
self.log.write(str(data))
self.log.write("\n")
self.log.flush()
server = LoggingServer()
print(f"[SMTP {HOST}:{PORT}]")
print("[waiting for email]")
asyncore.loop()
__EOF__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment