Integration between Bandwidth's API and Mailgun's API to provide email-sms communication
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Python version = 3.6.5



This guide makes the following assumptions:

  • You already have a Bandwidth account (
  • You already have a Mailgun account (
  • You already have a ngrok account (
  • You have python3, pip, ngrok, and virtualenv installed on your system.
  • You have purchased a Bandwidth number, and have a domain to use for Mailgun (Mailgun's default sandbox domain will be sufficient for a tutorial).
  • You have an email address to use as your destination email.

This guide shows how to set up a bidirectional Email-SMS integration using the Bandwidth messaging API and the Mailgun API hosted locally using ngrok. This app allows an end user to send a text to a Bandwidth number, and receive the contents of that text as an email to an email of your choice. Replying to that email will send a text to the end user with the contents of the reply.

Step 1: Open up a ngrok instance

Open up a terminal and execute the following command:

ngrok http 8000

Take note of the forwarding urls. You will need these later in the setup. Leave this terminal running, and continue the rest of this guide on a new terminal.

Step 2: Set up the Python virtual environment

Open up a new terminal. Create a new Python virtual environment by executing the following command:

virtualenv -p python3 <path_to_virtual_environment>

Activate the virtual environment by executing the following command:

source <path_to_virtual_environment>/bin/activate

Your terminal should show the name of your virtual environment on the prompt.

Run the following commands to install the project requirements via pip:

pip install django
pip install bandwidth-sdk
pip install requests

Step 3: Clone the project repo locally

Execute the following commands:

git clone
cd Email-SMS-Integration

Step 4: Set up a Bandwidth app to make POST requests on your server

  1. Go to
  2. Login to your account.
  3. Go to the 'Applications' tab.
  4. Click '+ Create New' button (top left of the page if you already have an app, otherwise in the center of the page).
  5. Name your application whatever you want (I would suggest something related to email-sms-integration).
  6. Set the 'Callback request method' to POST.
  7. Set the 'Application Type' to 'Messaging'.
  8. In the 'Messaging callback URL' box, type <your_ngrok_url>/send_mailgun_email/.
  9. Click the 'Create' button.
  10. Go to the 'Numbers' tab.
  11. Assign one of your Bandwidth numbers to your new Bandwith application. This is the number that your end users will text.

Step 5: Set up a Mailgun route to make POST requests on your server

  1. Go to
  2. Login to your account.
  3. Go to the 'Routes' tab.
  4. Click 'Create Route'.
  5. Under 'Expression Type', select 'Match Recipient'.
  6. In the 'Recipient' box, type .*@<your_domain_name>.
    • Note: If your domain name will be used for more than just this email-sms integration, you may want a more sophisticated regular expression. If you are using this domain only for the email-sms integration, the catch-all expression will be sufficient.
  7. Under 'Actions', select 'Forward'.
  8. In the 'Forward' box, type <your_ngrok_url>/send_bandwidth_message/.
  9. Click the 'Create Route' button.

Step 6: Assign environment variables

The following environment variables need to be assigned:


A django secret key can be generated by executing the following in a Python shell:

>>>from import utils
<secret key>

For using ngrok, HOST_URL is found on your terminal running the ngrok session. Do not include the 'http://' or 'http://' part of the url (Note: you do include the 'http://' or 'https://' parts when setting the url in Bandwidth and Mailgun).

Your Bandwidth user id, api token, and api secret values can be found on by clicking the 'Account' tab. BANDWIDTH_NUMBER is the Bandwidth number assigned to your Bandwidth application. This is the number that you want end users to send texts to and receive texts from.

Your Mailgun api key can be found on by clicking your account name on the top right corner and going to the security tab. MAILGUN_EMAIL_DOMAIN is the domain linked to your mailgun account. DESTINATION_EMAIL is the email (personal, business, etc.) that you want to receive messages from your end users.

Set DEBUG to False

Step 7: Start the server

Execute the following command python runserver

If you set your environment variables properly, you should see this:

Performing system checks...

System check identified no issues (0 silenced).
June 07, 2018 - 20:25:02
Django version 2.0.6, using settings 'email_sms_integration.settings'
Starting development server at
Quit the server with CONTROL-C.

You can now text your Bandwidth number, and you should receive an email from your Mailgun domain on your destination email. Replying to this email will send a reply text to the initial sender.