Tiliman’s Weblog

October 21, 2011

Sending Unicode SMS via Kannel

Filed under: GSM — Tags: , , , — tiliman @ 3:36 pm

Kannel is always a delight to work with. It is solid piece of software that would win over any commercial SMS GW.

Standard GSM characters are usually enough for English and most European languages but when it comes to sending SMS in other languages you have to rely on UNICODE. Here is character sheet of GSM characterset http://www.csoft.co.uk/sms/character_sets/gsm.htm
It might seem tricky at first to send Unicode SMS via Kannel and usually requires trial and error.

Sending unicode SMS involves following
1– Converting your SMS body string from whatever character set to into UNICODE (UTF-16).
2– URL Encoding (Percent Encoding) the SMS body.

HTTP GET example for sending Unicode SMS

<?php
$in_msg = "你好朋友Michael";
print urlencode(iconv('utf-8', 'ucs-2', $in_msg));
?>

My text here is stored in UTF-8 so I have to first convert it to UNICODE and then convert to URLencoded format.
The result comes out to be something like this
%60O%7DY%0Bg%CBSM%00i%00c%00h%00a%00e%00l%00
Now in Kannel send URL I have to specify that I am sending it UNICODE and for that I used charset=UCS-2
and to tell Kannel to send SMS as UNICODE as well (not as GSM characters)&nbsp;coding=2
I use HTTP GET URL for testing and this is how my final URL looks like
lynx –dump “http://KANNEL_SERVER:13003/cgi-bin/sendsms?username=USERNAME&password=PASSWORD&from=YOURFROMID&to=MOBILE_NUMBER&text=%60O%7DY%0Bg%CBSM%00i%00c%00h%00a%00e%00l%00&coding=2&charset=UCS-2&#8221;

HTTP XML POST example for sending Unicode SMS

XML POST is a little different indeed. The charcterset is actually identified in encoding param of xml namespace tag. You need to provide it the body in UNICODE (UTF-16/UTF-16BE) in URLEncoded form.


<?php
$SERVER = 'KANNEL_SERVER';
$PORT = '13003';

$out_msg = "你好朋友Michael怎么了";

# convert from UTF-8 to UTF-16.
$out_msg = mb_convert_encoding($out_msg,"UTF-16","UTF-8");

# Don't forget to URLEncode or else you might get unwanted string termination
$out_msg =  urlencode($out_msg);

print $out_msg."\n";
print "len ". strlen($out_msg)."\n";

$fp = fsockopen ($SERVER, $PORT, $errno, $errstr, 30);
    $res = "";
    $header = "POST /cgi-bin/sendsms HTTP/1.1\r\nHost: $SERVER\r\nContent-Type: text/xml; charset=utf-8\r\nContent-Length: __LENGTH__\r\n\r\n";
    $re1 = "";
    $req = "
<message>
    <submit>
        <da>
            <number>YOUR_NUMBER</number>
        </da>
        <oa>
            <number>FROM_ID</number>
        </oa>
        <ud>$out_msg</ud>
        <udh/>
        <dcs>
            <coding>2</coding>
        </dcs>
        <from>
            <username>USERNAME</username>
            <password>PASSWORD</password>
        </from>
    </submit>
</message>"; 

    $header = str_replace("__LENGTH__",strlen($req),$header);
    if (!$fp) {
            // HTTP ERROR
            print("Error in service.\nPlease try again later..");
            exit;
    } else {
            print "Sending\n$req\n";
            fputs ($fp, $header . $req);
            // read the body data
            $res = '';
            $headerdone = false;
            if (!feof($fp)) {
                $line = fread ($fp, 4096);
                print "Read:\n$line\n";
                fclose($fp);
            }

    }
?>

This is how your XML should look like

<?xml version="1.0" encoding="utf-16"?>
<message>
    <submit>
        <da>
            <number>MOBILE_NUMBER</number>
        </da>
        <oa>
            <number>YOURFROMID</number>
        </oa>
        <ud>O%60Y%7Dg%0BS%CB%00M%00i%00c%00h%00a%00e%00l%60%0ENHN%86</ud>
        <udh/>
        <dcs>
            <coding>2</coding>
        </dcs>
        <from>
            <username>USERNAME</username>
            <password>PASSWORD</password>
        </from>
    </submit>
</message>

This is all. Sending Unicode SMS is not that hard with Kannel after all.
Advertisements

3 Comments »

  1. Thx for the tips… i was searching for free sms service … this gonna help me … thx again\

    Comment by shaista — October 22, 2012 @ 7:39 am

  2. how can i send binary sms?

    Comment by riyas — March 20, 2013 @ 9:52 pm

  3. Unfortunately i get “Invalid charset” using your example. Any idea?

    Comment by Evans — December 10, 2013 @ 7:43 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: