Using the python SMTP DebuggingServer to test SMTP communications

During the development of the dns-domain-expiration-checker script I needed a way to test SMTP mail delivery w/o relying on an actual mail exchanger. While reading through the smtplib and snmpd documentation I came across the SMTP debugging server. This nifty module allows you to run a local mail relay which will print the messages it receives to standard out. To enable it you can load the smtpd module and instruct it to run the DebuggingServer command on the IP and port passed as arguments:

$ python -m smtpd -c DebuggingServer -n localhost:8025

This will fire up a local mail server on localhost:8025 and each messaged received will be printed to STDOUT. If I run my DNS domain expiration script and point it to localhost:8025:

$ --domainname --email --expiredays 2000 --smtpserver localhost --smtpport 8025 --email

The debugging server prints the SMTP headers and body each time the script generates an e-mail:

$ python -m smtpd -c DebuggingServer -n localhost:8025

---------- MESSAGE FOLLOWS ----------
Content-Type: multipart/mixed; boundary="===============3200155514135298957=="
MIME-Version: 1.0
From: root
To: root
Subject: The DNS Domain is set to expire in 1041 days

Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

Time to renew
------------ END MESSAGE ------------

Super useful module for troubleshooting SMTP and e-mail communications!

This article was posted by Matty on 2017-08-17 14:31:00 -0400 EDT