In 2013 72.3% of customers reported using a coupon during a purchase. Yet consumers are increasingly digital and transient. MMS coupons instead of paper printouts provide an alternative way to engage with phone-wielding customers.

In this tutorial we’ll walk through how to build a service to distribute branded MMS coupons using Python and Flask. An MMS coupon will be a message with an image that combines the logo of your choice with a scannable barcode. The resulting text message will appear on the user’s phone as shown in the following image.

When the coupon receiver expands the message they’ll see a full image like this one below.

What You’ll Need

To build the branded coupons app we’ll use the three following technologies.

If you are anxious to see the results of this tutorial and want to deploy the code right now, try out the app with this Heroku deploy button. All of the code for this tutorial is also stored in an open source GitHub repository.

There are five sections to this coupon app tutorial.

Create the structure of the coupon-creating Flask web app Write code to handle user input and HTML templates Use Pillow and PyBarcode to generate a branded coupon image Build the code to send the coupon image to a customer via MMS Deploy our coupon app to Heroku

Let’s get started incrementally building our coupon generator by creating the structure of our Flask app.

Flask web application structure

First set up the Flask app. If you don’t want to type up this code yourself there’s a tutorial-step-1 tag with these files already created.

In this section we’ll create the following list of files and directories.

requirements.txt – specifies necessary Python libraries

app.py – runs the dev server for our Flask app locally

coupons/ – contains the Flask app that generates and sends coupons

Let’s fill out the dependencies in the requirements.txt file. Each of these dependencies has several other libraries it relies upon which is why this list is shorter than the one you’ll see in the repository.