skip to Main Content

A simple Python email gateway

Say you have a Django web application that you want to integrate with emails to make it possibile to send data and files to your web application over SMTP.

The good news is that Python has a simple SMTP daemon in the standard library, together with modules to parse emails. Let’s see how to create a simple email gateway on top of these tools.

Using the following Python module you can setup a simple pure Python email server on port 25, parse the incoming emails and make a multipart/form-data POST HTTP request to your web application. You’ll need the awesome Requests library for the HTTP request to work.

In a production environment you can setup supervisor to launch your simple SMTP server. With a simple configuration like this:

command = /home/ubuntu/.virtualenvs/venv/bin/python /home/ubuntu/mail_gateway/ 
user = root

Then try to send an email to your server, if the server public IP has been associated to a DNS record you can use something like [email protected] as a recipient, and you should see the incoming email in the logs.

Have fun with emails!


Freelance developer and sysadmin

This Post Has One Comment

Leave a Reply

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

Back To Top