Python Smtpd Module
The smtpd module included classes for implementing SMTP servers.
Note: The smtpd module is deprecated as of Python 3.6 and was removed in Python 3.12. The recommended replacement is aiosmtpd.
Importing the Module
import smtpd
import asyncore
Creating a Debugging Server
A common use case for smtpd was to run a local debugging server that prints incoming emails to stdout.
Command Line
python -m smtpd -n -c DebuggingServer localhost:1025
Programmatic Usage
To create a server programmatically, you would subclass smtpd.SMTPServer.
import smtpd
import asyncore
class CustomSMTPServer(smtpd.SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data, **kwargs):
print('Receiving message from:', peer)
print('Message addressed from:', mailfrom)
print('Message addressed to :', rcpttos)
print('Message length :', len(data))
return
server = CustomSMTPServer(('127.0.0.1', 1025), None)
try:
asyncore.loop()
except KeyboardInterrupt:
pass