Native ways of testing emails in PythonÂ
Letâs start with the native ways of testing Python emails.Â
Using unit tests and aiosmtpd Â
Aiosmtpd is a library that lets you set up a local SMTP (Simple Mail Transfer Protocol) server. This will create a testing environment and handle email traffic internally. So, your test emails wonât reach the recipients. Refer to aiosmtpdâs GitHub page for more information.Â
Iâll create a test for a simple text email.Â
Prerequisites:Â
- Python 3.7 and up.Â
Note: I added the code snippets below in the tests.py file.Â
To install aiosmtpd, run the pip install aiosmtpd command.Â
Run the server with python -m aiosmtpd -n command.Â
Iâll use the unit test framework, a standard library in all versions of Python since 2.1, and the smtplib library. smtplib module uses standard RFC 821 protocol for formatting messages.Â





























