Hack93.Send SMS Messages from Your IM Client


Hack 93. Send SMS Messages from Your IM Client

Use PHP to send SMS messages to a cell phone from Jabber messages from your instant messenger client.

One of the most useful features of instant messaging is the availability of user presence (knowing whether a user is online). Of course, if a user is unavailable and you really want to get a message to him, it would probably be really cool if you could send the message to his mobile phone.

While many of the latest mobile phones are beginning to support instant messaging, nearly all of them support SMS text messaging. It would be great to send an instant message to a user's cell phone, and this hack shows how to do just that, by using an email-to-SMS gateway (something almost all major phone providers already have in place).

9.9.1. The Code

Save the code in Example 9-12 as smsclient.php.

Example 9-12. An SMS client in PHP
 <?php /* CONFIG VARIABLES */ // jabber server you are registed at // jabber server you are registed at $SERVER = 'yourserver'; //username and password for your special account $USERNAME = 'yourusername'; $PASSWORD = 'yourpassword'; // jabber id for your personal account $PERSONAL = 'username@yourserver'; //these values may change global $cingular; global $verizon; global $nextel; global $tmobile; global $ATT; $cingular = '@cingularME.com'; $verizon  = '@vtext.com'; $nextel   = '@messaging.nextel.com'; $tmobile  = '@tmomail.com'; $ATT      = '@mmode.com'; //store any numbers and their carriers here  global $cell;  //put any ten digit number and the carrier corresponding  // to the globals above as name-value pairs here  //e.g "2125551234" => "cingular"  $cell = array(               "10digitnumber" => "carrier"               );  /* END CONFIG */ function send($to, $msg) {    global $JABBER;    $JABBER->SendMessage("$to","normal", NULL, array("body" => htmlspecialchars($msg)),$payload);  }  //overrides jabber.class.php handler function Handler_message_normal($message) {    global $JABBER;    $body = $JABBER->GetInfoFromMessageBody($message);    if (substr ($body ,0,3) == SMS) {       $bodyparts = explode(":", $body);   $tokenparts = $bodyparts[1];   $tokens = explode(" ", $tokenparts, 3);   $num = $tokens[0];   $sub = $tokens[1];   $bod = $tokens[2];   sms($num, $sub, $bod);   }  } function sms($number, $subject, $body) {      global $cingular;  global $verizon;  global $nextel;  global $tmobile;  global $ATT;  global $cell;  switch($cell[$number]) {  case "cingular":         $suffix = $cingular; break;     case "verizon":         $suffix = $verizon; break;     case "nextel":         $suffix = $nextel; break;     case "tmobile":         $suffix = $tmobile; break;     case "ATT":         $suffix = $ATT; break;     } $address = $number.$suffix; mail($address, $subject, $body); echo $address.$subject.$body; } function Handler_message_chat($message) {    Handler_message_normal($message);  } require("class.jabber.php"); $JABBER = new Jabber; $JABBER->server         = $SERVER; $JABBER->port    = "5222"; $JABBER->username    = $USERNAME; $JABBER->password    = $PASSWORD; $JABBER->resource    = "smsclient.php"; $JABBER->enable_logging = FALSE; $JABBER->Connect()      or die("Couldn't connect!"); $JABBER->SendAuth()     or die("Couldn't authenticate!"); $JABBER->SubscriptionAcceptRequest($PERSONAL); while(true) {     $JABBER->SendPresence(NULL, NULL, "online");  $JABBER->CruiseControl(15 * 60);  } // may never get here but…  $JABBER->Disconnect(); ?> 

9.9.2. Running the Hack

First, you'll need to have a few Jabber accounts set up and have class.jabber.php downloaded [Hack #88] and placed inside your hack directory.

Next, edit the configuration variables at the top of the script, which contain the values of the formats of the email-to-SMS gateways to the major wireless providers; these variables are bolded in the code listing. Save the script as smsclient.php and put it in a directory with class.jabber.php.

The values defined for each cell phone network work at the time of this writing, but keep in mind that mergers and other changes to the volatile business landscape of wireless carriers might cause these values to change. You might have to retrieve updated information from your carrier's web site.


It is interesting to note that you need to know the carrier in advance. In light of recent laws allowing users to keep their cell numbers when switching carriers, there is really no way to store this information and be even moderately accurate. You could optionally choose to blast the message out to all carriers; that's kind of a sloppy solution, but then again, this is a hack!

Add the Jabber account you are using to connect with the script to your personal Jabber account contact list. Run the script from the command line:

 php smsclient.php & 

Messages sent to a cell phone will take the following format:

 SMS:10digitnumberSubject Body 

Just type in your message, and enjoy!

Matthew Terenzio

9.9.3. See Also

  • "IRC Your Web Application" [Hack #89]

  • "Send RSS Feeds to Your IM Application Using Jabber" [Hack #88]



PHP Hacks
PHP Hacks: Tips & Tools For Creating Dynamic Websites
ISBN: 0596101392
EAN: 2147483647
Year: 2006
Pages: 163

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net