Using Python built-in SMTP server for development and troubleshooting

Using Python built-in SMTP server for development and troubleshooting

Python has a built-in SMTP module, which can act as an email SMTP server and can be started on your workstation for development or troubleshooting purposes. It doesn't actually send the email to the target destination - emails are discarded and printed to the stdout.

Photo by Davide Baraldi

Requirements

The only requirement is the correct installation of Python.

Start SMTP server

Open command prompt (for Windows: Win+R -> cmd; for Linux: open terminal) and run the next command:

python -m smtpd -c DebuggingServer -n 127.0.0.1:1025

This will start the SMTP server and binds TCP port 1025 to the loopback interface of your host. You can replace 127.0.0.1 with any of the IP addresses for this host (like for example 10.1.1.1), or with the host DNS name (test.com), or with 0.0.0.0 to listen on all interfaces. After the command is executed, there will be no output in the CLI. The server now waits for the new connections.

Sending test email

Now to test our SMTP server we will send a test email message using telnet. You can read more about how to use telnet to send emails here:

Sending emails with Telnet
Little note on how to verify SMTP server reachability and how to send emails with Telnet app with and without authentication.

Open another command prompt and type the next commands:

telnet 127.0.0.1 1025

You will see the output of the telnet command trying to connect to the specified host and port and the server welcome message:

Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
220 test.com Python SMTP proxy version 0.3

Now we need to identify ourselves, type:

ehlo remote.test.com

The server will reply with its capabilities:

250-v-dc1-log02.dtekgroup.tek.loc
250-8BITMIME
250 HELP

Set email sender and recipients with the mail from: and rcpt to commands:

mail from: user@remote.test.com
250 OK
rcpt to: admin@test.com
250 OK

Email addresses in this case do not really matter, they can be non-existent.

Enter the message body by sending first the data command:

data
354 End data with <CR><LF>.<CR><LF>

followed by the actual Subject of the email and its contents:

Subject: Test email

Hello! This is the test
Hope you will see it.

- User

Finally, enter one blank line and finish the body input with the single .:


.
250 OK

Now you can enter the quit command to end the session.

In the first command prompt window, where we started the SMTP server, you will see the message:

---------- MESSAGE FOLLOWS ----------
b'Subject: Test email'
b'X-Peer: 127.0.0.1'
b''
b'Hello! This is the test'
b'Hope you will see it.'
b''
b'- User'
------------ END MESSAGE ------------

It means that you did everything right.


Share Tweet Send
0 Comments
Loading...
You've successfully subscribed to Prox blog
Great! Next, complete checkout for full access to Prox blog
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.