Created
May 17, 2023 15:32
-
-
Save manhg/0d83438f77330374ab82a4101cb4b285 to your computer and use it in GitHub Desktop.
Simple SMTP to debug emails
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
# 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