Sending emails with Python: Sparkpost API

July 03, 2017

Sparkpost API Docs
Sparkpost Python Git repo
Sparkpost Python Client Library Docs

Sparkpost has a generous free email quota (100,000 per month as of this writing).
I’m using this to send notifications from the inventory system, which doesn’t have a notification system.

First use case: Send weekly notification to show what shipments are still on the water i.e. in transit.
Currently the status of shipments can be obtained by going to each supplier invoice and seeing the shipment status and ETA.
There are also reports for On PO, In Transit and On Hold, but doesn’t show the relevant dates.
Deeper scraping is required for that.

Code Organization

This point seems to come up repeatedly. Since I’m building a package freestyle, code organization needs much more thought that when using a framework.
I anticipate using Flask to build an API around it, however, I am considering using Django apps to separate out logic for the different parts.

Right now, I’m adding a class called ‘Email’ in the main module to handle email creation and sending.
It uses Sparkpost’s SMTP relay (just requiring an API key), once DKIM, tracking and bounce domains are setup.

Attaching reports is clumsy right now. In my I generate the reports before attaching them to my email and sending it.

There’s no check to see if the reports are generated correctly. There is also no way to modify the default reports currently.