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:
[program:smtp_server] command = /home/ubuntu/.virtualenvs/venv/bin/python /home/ubuntu/mail_gateway/smtp_server.py user = root
Then try to send an email to your server, if the server public IP has been associated to a DNS record yourserver.example.com you can use something like email@example.com as a recipient, and you should see the incoming email in the logs.
Have fun with emails!