Debug SMTP Server One-Liner
If you are doing web development there is often a need to emulate and intercept outgoing email. Email delivery is handled by SMTP protocol. Production and staging server have fixed SMTP servers available in their network. However, this is not often the case for your development laptop, especially if you tend to do development in different networks (places).
Python comes with simple smtpd module which allows you to run a simple SMTP server easily. When doing email debugging, smtpd has extreme useful DebuggingServer feature making it dump all relayed email to stdout instead of relaying them forward for email delivery. This approach is compatible on all platforms: Windows, OSX and Linux.
Non-root way to run debugging SMTP server. You need to change the SMTP server details in your application settings to localhost:1025:
python -m smtpd -n -c DebuggingServer localhost:1025
If you want to bind SMTP port 25 you need to run this as a root:
sudo python -m smtpd -n -c DebuggingServer localhost:25
Now, when your application sends email it is printed to terminal running debugging smtpd:
---------- MESSAGE FOLLOWS ---------- Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [example.com] Confirm your email From: firstname.lastname@example.org To: email@example.com Date: Fri, 26 Apr 2013 07:20:34 -0000 Message-ID: <20130426072034.83439.2762@Kohr-Ah.local> X-Peer: 127.0.0.1 Test output from smtpd ------------ END MESSAGE ------------
Alternative you can go for a full stack solution and install Postfix with external authenticating mail server.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)