Python Built-in Local SMTP Server Example

Python has a built-in smtpd module, this module can act as an email SMTP server which run on localhost or any specified email domain.  But it do not really send the email out to the target email server, it just discard the email and print out the email content on the console. This article will show you how to use this Python smtpd module to start a SMTP email server and how to use email client Thunderbird to send email through the server.

1. Start SMTP Server With Python Smtpd Module.

1.1 For Windows.

  1. Open a dos window. Make sure python has been installed correctly. If not, please refer article How To Install Python/Pip On Windows.
  2. Run below command in dos window. Please note the domain name test.com, this SMTP server domain will be smtp.test.com. It will listen on port number 1025. You can specify any domain and port number as you like.
    python -m smtpd -c DebuggingServer -n test.com:1025
  3. Now the SMTP server will be waiting for any email send through it. There will have no output in the dos window.

1.2 For Linux.

  1. Open a terminal and run below command to start the SMTP email server.
    $sudo python -m smtpd -c DebuggingServer -n test.com:1025

2. Send Email From Thunderbird Use Above SMTP Server.

  1. First add below ip domain mappings in hosts file. Please refer How To Edit Hosts File In Windows 10 . Then Thunderbird will resolve smtp.test.com to ip address 127.0.0.1 when it send the email through above SMTP email server.
    127.0.0.1 test.com
    127.0.0.1 smtp.test.com
    127.0.0.1 pop3.test.com
  2. Download and install Thunderbird. Please refer article How To Connect Localhost Apache James With Thunderbird section 4.Use Thunderbird To Connect Apache James Mail Server On Localhost to add email account and configure the email account SMTP server as smtp.test.com, the SMTP server port number as 1025.
  3. To create an email account in Thunderbird, you need both a SMTP server and POP3 server running. So you can first create Apache James server to start SMTP and POP3 server that are used for setting up email account in Thunderbird ( this is introduced in How To Connect Localhost Apache James With Thunderbird).
  4. Then right click the email account in Thunderbird, click Settings menu item.
    right click thunderbird email account settings menu item
  5. Then click the Outgoing Server menu item in left panel, and add the python built-in smtpd server in right panel like below. And set it as the default outgoing server.
    edit thunderbird outgoing smtp server to python built-in smtpd server
  6. Now write an email in Thunderbird and send it to any email address, then you can find below output in the SMTP server terminal window.
    C:\>python -m smtpd -c DebuggingServer -n test.com:1025
    ---------- MESSAGE FOLLOWS ----------
    mail options: ['BODY=8BITMIME']
    b'To: [email protected]'
    b'From: admin <[email protected]>'
    b'Subject: Hello from [email protected]'
    b'Message-ID: <[email protected]>'
    b'Date: Thu, 13 Dec 2018 15:01:12 +0800'
    b'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101'
    b' Thunderbird/60.3.3'
    b'MIME-Version: 1.0'
    b'Content-Type: text/plain; charset=utf-8; format=flowed'
    b'Content-Transfer-Encoding: 7bit'
    b'Content-Language: en-GB'
    b'X-Peer: 127.0.0.1'
    b''
    b'Hello this is [email protected] speaking.'
    ------------ END MESSAGE ------------
READ :   Python Send Email To Multiple Contact In CSV File With Personalized Content Example

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.