Skip to content

Instantly share code, notes, and snippets.

@manhg
Created May 17, 2023 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manhg/0d83438f77330374ab82a4101cb4b285 to your computer and use it in GitHub Desktop.
Save manhg/0d83438f77330374ab82a4101cb4b285 to your computer and use it in GitHub Desktop.
Simple SMTP to debug emails
# inspired by Ruby's mailcatcher but simple drop to run version in Python
# check logs for email content
import asyncore, email
from smtpd import SMTPServer
class MyServer(SMTPServer):
def process_message(self, peer, mailfrom, *args, **kwargs):
print('====')
print('= ')
print(mailfrom, peer)
print('TO', args[0])
msg = email.message_from_bytes(args[1])
for header in msg.items():
print(header)
for part in msg.walk():
print(part.get_content_type())
data = part.get_payload(decode=True)
if data:
print(data.decode('utf8'))
print(kwargs)
port = int(os.environ.get('SMTP_PORT', 1025))
server = MyServer(('localhost', port), None)
print("Listen on", port)
try:
asyncore.loop()
except KeyboardInterrupt:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment