I needed a development SMTP server that I could use for testing with Microsoft Outlook, which would save the e-mails in a folder so I could go back to them. I was already aware of
python -m smtpd -n -c DebuggingServer localhost:25, but this solution allows me to open the results in Outlook.
I use this with Cygwin on Windows.
import smtpd import asyncore import io import uuid import os class FileSMTPServer(smtpd.SMTPServer): def process_message(self, peer, mailfrom, rcpttos, data): f_name = os.path.join('mailroot', str(uuid.uuid4()) + '.eml') with open(f_name, "w") as out_file: out_file.write(data) if not os.path.isdir('mailroot'): os.mkdir('mailroot') print("Development SMTP Server Listening on Port 25") server = FileSMTPServer(('127.0.0.1', 25), None) asyncore.loop()
Save it as
smtp-test-server.py, and run it with
python smtp-test-server.py. It’ll create a folder called
mailroot which will store each e-mail with a unique ID.
Note that you’d need to run this with
sudo on Linux, or change the port number (port 25 in this example) to one above 1024.