{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner
architects,devops,tool,tips and tricks,tools & methods,smtp

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: test@example.com
To: mikko@example.com
Date: Fri, 26 Apr 2013 07:20:34 -0000
Message-ID: <20130426072034.83439.2762@Kohr-Ah.local>

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 {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks