Test SMTP with a dummy server in Python

October 13th, 2011

Warning: This post is 12 years old. Some of this information may be out of date.

Today I came across a need to test SMTP outgoing emails from the web application I was working on. I was working on a simple forgotten password form that sent a password reset url to the registered user's email address. I didn't need to test that the email looked OK, I just needed to test the content.

Instead of setting up Exim or Sendmail and configuring it to redirect mail to my account I discovered that you can use a Python one liner to output all SMTP to the command line. Here's how it's done:

Test SMTP with a dummy server in Python

sudo python -m smtpd -n -c DebuggingServer localhost:25

The sudo is needed as you can't bind to a port number lower than 1024 as a normal user.