The One About Testing Email With Python

From time to time I have found myself working on Python projects where I need to send out emails. Most, if not all, of the time I don’t have an email server running on my dev machine and I don’t want to use a real email sever to just for testing.

Fret not, Python have you covered! Thanks to Python’s SMTP debugging server (an smtp proxy). Using this module, you can test that functionality without actually using a real email server. Just open a new console window and run the following to start a fake email server:

$ python -m smtpd -n -c DebuggingServer localhost:8025
  • -m <module name>: Runs the library moudle by the provided name.
  • -n or -nosetuid: This flag used to prevent the setuid call from failing
  • -c or -classname <classname>: Class to use as the concrete SMTP proxy class. “PureProxy” is used by default.
  • if you omit localhost:localport, localhost:8025 is used by default.

There are other options, so be sure to check out the docs for the SMTP module for Python.