DevOps Zone is brought to you in partnership with:

A friendly Finnish hacker. I am technology consultant, open source advocator and entrepreneur. My expertise areas cover HTML5, Python, Plone, Javascript, WebGL,UNIX and mobile web. Mikko likes sushi, Angry Birds and dislikes winter. Mikko is a DZone MVB and is not an employee of DZone and has posted 43 posts at DZone. You can read more from them at their website. View Full User Profile

Debug SMTP Server One-Liner

04.27.2013
| 5906 views |
  • submit to reddit

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: test@example.com
To: mikko@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.

Published at DZone with permission of Mikko Ohtamaa, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)