Python has a built-in SMTPD module, this module can act as an email SMTP server that runs on localhost or any specified email domain. But it does not really send the email out to the target email server, it just discards the email and prints out the email content on the console. This article will show you how to use this Python SMTPD module to start an SMTP email server and how to use the email client Thunderbird to send email through the SMTP server.
1. Start SMTP Server With Python Smtpd Module.
1.1 For Windows.
- Open a dos window. Make sure python has been installed correctly. If not, please refer article How To Install Python/Pip On Windows.
- Run below command in a 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
- Now the SMTP server will be waiting for an email send through it. There will have no output in the dos window.
1.2 For Linux.
- Open a terminal and run the 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.
- First, add below IP domain mappings in the OS hosts file. Please refer to How To Edit Hosts File In Windows 10. Then Thunderbird will resolve smtp.test.com to IP address 127.0.0.1 when it sends the email through the above SMTP email server.
127.0.0.1 test.com 127.0.0.1 smtp.test.com 127.0.0.1 pop3.test.com
- 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 an email account and configure the email account SMTP server as smtp.test.com, the SMTP server port number is 1025.
- To create an email account in Thunderbird, you need both an SMTP server and a POP3 server running. So you can first create an Apache James server to start SMTP and POP3 server that is used for setting up an email account in Thunderbird ( this is introduced in How To Connect Localhost Apache James With Thunderbird).
- Then right-click the email account in Thunderbird, click the Settings menu item.
- Then click the Outgoing Server menu item in the left panel.
- Click the Add button on the Account Settings window right panel, then it will popup the Add SMTP Server dialog.
- Input the python built-in SMTPD server information ( Description: python built-in smtpd, Server Name: localhost, Port: 587, Connection security: None, Authentication method: Password, transmitted insecurely, User Name: ) in the popup dialog, click the OK button to save it.
- Select the newly added SMTP Server, click the Set Default button to set it as the default outgoing server.
- Now write an email in Thunderbird and send it to any email address, then you can find the 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 ------------