SMS alerts are a very convenient way to convey small amounts of information in a way that works for most people in most places. I use them both to alert users of my applications and also for my own convenience. In today’s post, I’m making use of IFTTT (IF This Then That), a tool to link more or less anything to more or less anything else. If that sounds like an extravagant claim, just look at their homepage:

Today’s example is a very simple one using incoming email as the “event” that triggers an SMS, but I’ve also used IFTTT with calendars, various IoT sensors and webhooks so the possibilities are endless. If you haven’t played with IFTTT for any reason, then I’d thoroughly recommend it as a fun way to link up systems that don’t already know how to talk to one another.

Set up the SMS-sending code

I don’t need a server to send SMS, instead I’ll use a serverless function. Serverless is a way of deploying independent functions to the cloud and running them on demand. Most of the platforms are a pay-as-you-go model with a generous free tier, making this a very cost-effective way to run this low-volume application. If you’re new to serverless, check out some of these excellent introduction resources:

The example here uses PHP code and the IBM Cloud Functions platform, but the idea would work just as well in another programming language or using another serverless platform (if there’s a particular technology combination you’d like to see an example of, tell us on twitter and we’ll see what we can do).

The setup will look something like this:

IFTTT reacts to new email, sending a webhook to the serverless function. That function in turn calls Nexmo’s API to send an SMS to the desired phone number. To set this up, you need to deploy the serverless function and then configure IFTTT to send data to it.

Before you start

You will need:

A Nexmo account. You can sign up here if you don’t already have one.

An IBM Cloud account (or another installation of Apache OpenWhisk)

The ibmcloud tool with cloud-functions plugin (or wsk tool for an alternative Apache OpenWhisk installation)

With all those in place, we can begin!

Get the code

Everything you need is in a handy GitHub repository here: https://github.com/nexmo-community/email-to-sms. Clone this to your local machine (it’s not a lot of files, it just saves on copy-and-pasting)

Copy .env-example to .env and add the values for your Nexmo account (get these from the dashboard) and the number you want to send an SMS to.

Here’s the code in the serverless function that we’re about to deploy. I always think it’s prudent to read code before running it.

<?php require "vendor/autoload.php"; function main($params) { $body_data = base64_decode($params['__ow_body']); $client = new Nexmo\Client(new Nexmo\Client\Credentials\Basic( $params['apikey'], $params['apisecret'] )); $response = $client->message()->send([ 'to' => $params['tonumber'], 'from' => 'Email relay', 'text' => $body_data ]); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 < ? php require "vendor/autoload.php" ; function main ( $ params ) { $ body_data = base64_decode ( $ params [ '__ow_body' ] ) ; $ client = new Nexmo \ Client ( new Nexmo \ Client \ Credentials \ Basic ( $ params [ 'apikey' ] , $ params [ 'apisecret' ] ) ) ; $ response = $ client -> message ( ) -> send ( [ 'to' = > $ params [ 'tonumber' ] , 'from' = > 'Email relay' , 'text' = > $ body _ data ] ) ; }

The $params argument to main() contains both the incoming body data and the values from .env that you set earlier; we can use these values in our code. The code here uses the Nexmo PHP library to create a Nexmo\Client object that then builds and sends an SMS containing the text from the IFTTT webhook.

Install the dependencies

Before we can use the Nexmo PHP library, we need to install it using Composer.

Each serverless action can have its own dependencies. For the send_sms action used here, the dependencies are described in send_sms/composer.json . We can install the dependencies by running the command composer install from inside the send_sms directory.

Once the dependencies are there (you should see a send_sms/vendor/ folder appear), then we’re ready to deploy.

Deploy the serverless function

Deploy by running the script ./deploy.sh . Here’s that script so you can see what happens – or has already happened if you already ran the command.

#!/bin/bash . .env ibmcloud wsk package update email-to-sms -p apikey $NEXMO_API_KEY -p apisecret $NEXMO_API_SECRET cd send_sms zip -rq send_sms.zip index.php vendor ibmcloud wsk action update email-to-sms/send-sms --kind php:7.1 --web raw -p tonumber $TO_NUMBER send_sms.zip cd .. 1 2 3 4 5 6 7 8 9 10 11 #!/bin/bash . . env ibmcloud wsk package update email - to - sms - p apikey $ NEXMO_API_KEY - p apisecret $ NEXMO_API_SECRET cd send_sms zip - rq send_sms . zip index . php vendor ibmcloud wsk action update email - to - sms / send - sms -- kind php : 7.1 -- web raw - p tonumber $ TO_NUMBER send_sms . zip cd . .

What’s happening here? Well, something like:

Get the environment variables from the .env file we configured above. Create the package that our serverless function (usually called an “action” in serverless terminology) will be added to. Set the API credentials that will be needed. Create a zip file with the index.php file and also the contents of the vendor/ folder since we’re using an extra library. Deploy the function/action into the package. Since it’s a zip file, I set the --kind parameter to tell OpenWhisk which runtime to use. The --web raw parameter makes the action web-enabled, so we can make web requests to it, but doesn’t try to automatically process and extract incoming variables. The -p sets a parameter – this case the number to send to. For this example, all the messages will be sent to the same phone number but depending on your use case it might make more sense to pass this in with the webhook.

That function now exists in the cloud. You can check everything worked with a command like this:

ibmcloud wsk action list 1 2 ibmcloud wsk action list

Next, ask the action what URL it has:

ibmcloud wsk action get --url sms-by-email/send_sms 1 2 ibmcloud wsk action get -- url sms - by - email / send _ sms

The response will be a URL – copy it quick! You’re about to use it setting up the next part …

Setting up the trigger

I’m using Gmail as the input as an easy way of demonstrating everything working. There is an excellent selection of gmail options available to choose from:

I chose the first option to include all mail. Now it gets more interesting as we configure the resulting action.

Setting up the webhook

For the resulting action, choose the Webhook:

And then, using the URL you copied earlier, we can go ahead and configure the webhook:

The URL should already be in your clipboard and this example expects a POST method as is common for webhooks. Set the content type to application/json and then configure the Body as you wish, for example mine looks like this:

{{FromName}} sent: {{Subject}} 1 2 { { FromName } } sent : { { Subject } }

For this demo, the webhook will accept any and all data sent to it which is very easy to get going but is not wise for production. We’d recommend using an approach such as a shared secret so that you can verify the webhook is coming from where you think it is.

Try what you made

At this point, everything should be ready. So turn up the volume on your phone, email yourself, and wait for the SMS alert!

Using this approach, perhaps with different triggers or sending different data through to the serverless function, you can connect more or less anything to SMS with IFTTT as the “glue”. Let us know what you build, we love to hear your stories.

Where next?

Here’s a few related resources that you might like to read next: