Creating Email-SMS Integration Using Bandwidth and Mailgun

Email to SMS graphic

One could argue that smartphones have been one of, if not THE most impactful new technology in the 21st century. The ability to communicate with anyone anywhere in the world over WiFi or cellular data has shaped friendships, communities, businesses—even cultures—worldwide. Right now as I’m writing this, I can think of 7 different ways to immediately contact with my mom who lives over 250 miles away just using my smartphone.

 

For someone who owns a business, these multiple means of communication could get overwhelming. Phone calls. Voicemails. Text messages. Emails. Facebook messages. It can be a lot! Ideally you would want all means of communication to be sent to the same destination. This would reduce human error by allowing you to check only one destination for all communication. Email forwarding already exists for products like Facebook, so it would be reasonable to assume someone would want text messages forwarded to their email as well.

 

Using the Bandwidth messaging API and the Mailgun API, you can set up a bidirectional email-SMS integration that allows an end-user to send a text to a Bandwidth number and have that text forwarded to an email address of the user’s choice. The user can reply to this email, and that email will be forwarded via text message back to the end-user.

 

I created this email-to-SMS integration as a Python/Django app, and it you can check it out here. You can also find instructions to deploy my app using your own Bandwidth and Mailgun credentials in the README.md file.

Why use an email-to-SMS app?

Let’s say you’re a business owner that’s always on the go. You don’t have the resources to manage your several different methods of communication, especially if you travel out of the country where you may not even receive text messages. But your employees send you emails and text messages with their concerns. How great would it be if you only had to check your email knowing that you would receive text messages from your employees to your email? And that you could reply to these emails and have your response be sent as a text message to your employees? This is the power of email-SMS integration.

 

How it works

A server needs to be set up that accepts POST requests on 2 URLs. One URL would be for POST requests received from the Bandwidth messaging API, and the other URL would be for POST requests received from the Mailgun API. You need to set up applications in both your Bandwidth and Mailgun accounts to make these POST requests when receiving text messages and emails.

 

Receiving a POST request from the Bandwidth messaging API

When an end-user sends a text to a Bandwidth number that is linked to a Bandwidth messaging application, the Bandwidth messaging API makes a POST request to a URL on our server. The information that our messaging API sends to a server when making a POST request can be found here. Using this information, we can get the end-user’s message contents and phone number on our server and send an email using the Mailgun API to our desired email.

 

Receiving a POST request from the Mailgun API

Using the Mailgun API, you can define routes that execute some action when an email is sent to an email address on our domain. We can use one of these routes to make a POST request to a URL on our server (learn more about Mailgun’s routes). Using this information, you can get the contents of an email and the end-user’s phone number on our server and send a text using the Bandwidth messaging API to the end-user.

And that’s it. That’s all it takes to use Bandwidth’s messaging API to set up bidirectional email and text message communication. We’ve made this project available on Github so anyone can use it to build the next cool thing.

Check out the project