Despite SMS technology being almost 20 years old (the first official SMS was sent on December 3rd 1992 in the UK), there is still a huge problem with sending messages from applications. This is usually because there is no unified SMS API or there is no API at all, just SMS gateways.

Some time ago I was contacted by Pavel from TextMagic.com, a service that aims to solve this problem. While the TextMagic service is not free, I decided to write a tutorial about it since I believe many developers will find it useful.

This will be a pretty basic tutorial that shows how to send SMS messages to a WordPress administrator when a new post is being published (this might be useful when you have multiple authors on the blog).

Getting an API Key

Before getting started, you need to register your account at TextMagic. You will get 10 free SMS credits, which is quite nice because it allows you to test the service for free. When done, you need to log in and generate your API password. Finally, you need to apply for a SenderID (apply suggests that the Sender ID has to be approved by someone, but it seems it’s auto approved). Now we are ready to write some code.

You will find TextMagic API documentation at http://api.textmagic.com. PHP SDK is also available, however we won’t be using it since it does not fit our needs exactly, plus we will need just one API function.

Below is the source code with inline comments for a WordPress plugin that will send an SMS message when someone publishes or updates a blog post:

/*
Plugin Name: TM Example
Plugin URI: http://ditio.net
Description: TextMagic Example
Author: Greg Winiarski
Version: 0.01
Author URI: http://ditio.net
 
TextMagic SMS delivery example.
 
*/
 
add_filter('wp_insert_post_data', 'tm_insert', 0, 2);
 
function tm_insert($post) {
 
    if($post["post_status"] != "publish" || $post["post_type"] != "post") {
        // run only when post is published
        return $post;
    }
 
    $isUtf = false;
    $title = substr($post["post_title"], 0, 140);
 
    if (preg_match('!\S!u', $title)) {
        $isUtf = true;
    }
 
    $arr = array(
        "cmd" => "send",
        "username" => "your-username",
        "password" => "your-api-password",
        "text" => $title,
        // recipient phone number, starts with country prefix
        // should be just numbers, if you wish to send it to
        // multiple recipients you need to seperate numbers with comma
        "phone" => "48601xxxxxx",
        "unicode" => $isUtf,
        // from is the Sender ID you registered earlier
        "from" => "TM-Example",
        // make sure just one sms is sent
        "max_length" => 1
    );
 
    $query = http_build_query($arr);
    $result = wp_remote_get("http://www.textmagic.com/app/api?".$query);
    if(is_wp_error($result)) {
        // log error message somewhere
        $object = json_decode($result["body"]);
        $error = $object->error_message;
 
    } else {
        // sms sent
        $object = json_decode($result["body"]);
 
    }
 
    return $post;
}

Please note that TextMagic also offers an Email To SMS service that allows you to send SMS messages without any programming knowledge at all, which is very cool.

The tutorial is now complete; however, feel free to expand on it further by adding functionality that will allow you to check your account balance and SMS logs. Please see API documentation for more details on how to do this.