How to Turn Emails into Text Messages with Two Simple APIs

10 Aug

Building a bridge between enterprise email and consumer SMS for better customer experiences

Still using email? Yup, me too.  It’s one of the first things a new employee receives after starting their job; an email address. We can’t escape it, even though products like Microsoft Teams, Slack, RingCentral Glip, and Cisco Spark have made significant headway in reducing email clutter by enabling real-time collaborative communication in the enterprise. But what about business to consumer communications? Advanced CRM tools allow enterprises to multiple channels into a single view. Social Media (Twitter, Facebook, Instagram), Email, Phone calls – All presented in a single panel.

But what happens when your audience wants to engage via SMS? Not all Enterprise CRM platforms have enabled SMS capabilities and many mature enterprises still issue desk phones to every employee. ICMI reported “74% of consumers use three or more channels to access customer service”. With 4.7 Billion mobile users in the world and SMS native to every mobile handset, it’s definitely a communication channel worth paying attention to.

Why can’t you leverage an existing, well understood channel like email and still communicate with audiences that would prefer to engage via SMS?

Email is slow (by today’s standards), but engrained in nearly every business. SMS, by comparison is fast, with high read and fast response rates by users.

Creating the perfect Email / SMS Gateway

Let’s define what our ideal solution would be where the the transactional nature of SMS and descriptive nature of email communications can come together.

The ideal Email / SMS gateway would minimally have:

  • Ability to have two-way communications between an email address and any sms-enabled phone number
  • Ability to send long messages (beyond the normal 160 character limit of a typical SMS)
  • Ability to send and receive attachments
  • Fast! The user should not even be aware that SMS is getting translated to an email
  • Easy! Easy to configure, deploy, manage, and educate users on how to use


Sure, you could probably buy an
off-the shelf solution, but why do that when you can build your own in just a few steps?

Implementing your Email SMS gateway in 5 steps

Implementing an SMS-Email gateway sounds like a tall order. There are email clients, email servers, MTAs, phone numbers, messaging protocols, and enough acronyms to make alphabet soup jealous.

Fortunately we can narrow the scope of our problem by connecting the two communications channels through cloud based email APIs and Bandwidth’s CPaaS for messaging.

A detailed instruction set, including code is available here.

1. Configure Your Domain

You’ll need the help of your IT department here. First, setup a sub-domain for your enterprise that will handle all the email for your SMS-Email relay. Something like sms.mydomain.com works nicely as it is descriptive of the intended use. The reason you need a sub-domain is because we’ll need to delegate DNS MX records to a 3rd party.

2. Set up an Email API

For this exercise, we’ve decided to use Mailgun, though you could pick any transactional email provider with a solid API. We like Mailgun because of their extensive documentation and generous starter tier. Simply create an account. Add the sub-domain created in Step 1. Then perform the DNS verification process.

The last step is to setup a “route”. We’ll do that after we deploy our code.

When we are all done, we should have a nice looking dashboard with email stats published similar to this one:

3. Set up the Bandwidth Messaging API

Bandwidth SMS and MMS APIs are the obvious choice for this solution. If you don’t already have a developer account with Bandwidth, get one. You’ll need create a new “application” and allocate at least one phone number to the application. Each number you allocate will be enabled for your Email-SMS relay.

You’ll need to tell Bandwidth where your code is living so the platform knows where to notify you when a new SMS comes in. Since we’ve not deployed our code yet, we’ll come back to this step.

4. Grab the code!

We’ve provided a sample application that will create an Email-SMS relay service for you. The code is located at https://github.com/inetBrad/SMS-Email-Relay. All you need to do is host it on a server that allows public HTTP requests from Bandwidth and Mailgun.

There are a few items you’ll need to configure in the code before you can kick the tires.

  1. Add your API credentials
  2. Add a file called contacts.csv with your phone numbers <-> email address mappings.

You’ll also need to tell your API providers how to reach your code:

  1. Bandwidth – Set the callback URL to your web server in your configured application
  2. Mailgun – Create a catch_all() route that points to your web server.


5. Take it for a spin!

If everything was configured correctly, you should now have a fully functional Email-SMS relay.  Only users in the contacts.csv file are authorized to use the email relay. 

All you need to now is send an email. Create a new email with the following fields:

From: valid email address in contacts.csv
To: mynumber@sms.mydomain.com
Subject: phone number of recipient
Body: Whatever you want to appear in the text

Things you can try:

  • Send an email and watch the message appear as an SMS on your cell phone
  • Attach a picture to on outgoing email and see it appear on your cell phone
  • From your cell phone, send an SMS to your business number in contacts.csv and watch it appear in your email box


Did you build your own SMS/email gateway? Let us know your own results in the comments. I expect to see some major ease-of-use benefits, and hopefully an uptick in response rates.

Brad Roldan
Brad Roldan
BRoldan@bandwidth.com

Since joining in 2011, areas of focus for Brad Roldan have included initiatives designed to meet the needs of developers seeking to transform communications through innovative applications. Brad has led teams in Bandwidth's R&D, Product, and is now leading Sales Engineering for Messaging and APIs. Brad currently spends his time collaborating with customers on launching new messaging and voice solutions using Bandwidth's CPaaS offering. When he is not working at Bandwidth, you might find Brad grafting rare fruit trees. Brad has a degree in Computer Engineering from Cal Poly, San Luis Obispo.

2 Comments
  • Annette Collins
    Posted at 05:20h, 19 August Reply

    Tried out script works great. However, email to sms is not working. Possibly miss configuration. What should be entered in define(‘LOCALSERVER’, ‘http://localhost/’) . Should this be the website where the php files are hosted?

    • Brad Roldan
      Brad Roldan
      Posted at 21:27h, 22 August Reply

      Update LOCALSERVER with the URL of where you are hosting the PHP code. For example, in the live demo, the configuration looked like this:
      define(‘LOCALSERVER’, ‘http://damp-fjord-NNNNN.herokuapp.com/’); // <- Configure your URL here.

Post A Comment