How to send test email in Python

Introduction - sending email in Python

If you're writing software using Python, you're probably sending emails, either as part of sign up flows, or for sending notifications or maybe as notifications in some machine learning software.

The standard way to send email is via an SMTP server, usually hosted by a specialist email service provider but can also be hosted yourself.

Python provides two modules for dealing with email, the email module, for creating/parsing/using email messages, and the smtplib module, for sending emails over SMTP.

In order to send email using the smtplib you need to know the host and port of the SMTP server and then almost always a username and password with which to authenticate yourself

While a lot of languages hide the protocol details of SMTP, the smtplib can require you to have a bit more knowledge around the protocol, whether you need to use SSL/TLS, and which mechanism to use for logging in.

Here's the standard method for sending a text file over email, as per the python documentation:

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.message import EmailMessage

# Open the plain text file whose name is in textfile for reading.
with open(textfile) as fp:
    # Create a text/plain message
    msg = EmailMessage()

# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = f'The contents of {textfile}'
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server.
s = smtplib.SMTP('localhost')

Sending Test Email: Set up Imitate Email

The above code assumes you are sending emails to an SMTP server on the same machine (localhost). To use Imitate Email as a test email server, we need to specify the smtp server, the credentials and we want to use TLS encryption.

If you've not already done so please sign up to Imitate Email.

For Imitate Email, when sending test emails you use the following server details:

Port: 587

Imitate Email uses STARTTLS which ensures that your emails are sent over an encrypted connection.

Then, for your username and password you need to decide if you're using your personal mailbox or have set up a project in Imitate Email.

Locate your mailbox credentials

Your username

To locate your username go to Settings > My Mailbox where you will find it (it is the same for all mailboxes in your account)

For the password

If you're on the Free or Developer plan or you're setting this up for your personal mailbox copy the password from the same place as the username.

If you're setting this up for a team mailbox go to Settings > Projects and you will find the password next to the symbol for your mailbox. It will look like the following 6bcb69b2-08ac-4c67-911a-10442f7d84b3

Screenshot of Imitate Email personal settings

Now, to send an email to Imitate Email, use the following code, once you have your email:

s = smtplib.SMTP('', port='587')
s.starttls() # encrypts the connection
s.login('<username>', '<password>') # log in using credentials
s.sendmail(from, [to], msg.as_string())