SMS Application

Following scripts will guide you to send SMS using SMPP/HTTP method. I have tested that and it is working.

SENDING SMS VIA HTTP using PERL:

Where I am sending SMS by connecting with a DB fetching records from the table with status=0 and then sending sms using http api and updating the status=1.

=====================================================================================================

#!/usr/bin/perl

use strict;

use HTTP::Request;

use LWP::UserAgent;

use DBI;

my $i=0;

my @ps = `ps -ef | grep test.pl`;

###############################################

# Checking whether sendsms.pl process is already running or not

while ( my $ps = shift @ps )

{

if($ps =~ m/.\/sendsms.pl/){

$i=1;}

}

if($i==1)

{

print "Process is running";

exit;

}

print "Process is not running hence sending sms";

#########################################################

my($dbh) = DBI->connect("dbi:mysql:mktsms;host=localhost;user=sandip;password=test01") or die "Could not Connect to Database: DBI->errstr";

my($sSQL)= "select mobileno,message from sms where status=0";

my($sth)=$dbh->prepare($sSQL) or die "preparing MySQL query failed: $DBI::errstr";

$sth->execute();

while(my $row = $sth->fetchrow_hashref()) {

print "$row->{mobileno} $row->{message}";

$sSQL="update sms set status=1 where mobileno='".$row->{mobileno}."'";

my($sth1)=$dbh->prepare($sSQL) or die "preparing Update query failed: $DBI::errstr";

$sth1->execute();

my $userAgent = LWP::UserAgent->new();

my $request = HTTP::Request->new(POST => 'http://example.com/smsapi/MessageCompose?user=sandy@sandipdutta.in:test123&senderID=9XXXX&receipientno='.$row->{mobileno}.'&msgtxt='.$row->{message}.'&state=4');

my $response = $userAgent->request($request);

if($response->code == 200) {

print $response->as_string;

$sSQL="update sms set status=1 where mobileno='".$row->{mobileno}."'";

my($sth1)=$dbh->prepare($sSQL) or die "preparing Update query failed: $DBI::errstr";

$sth1->execute();

}else {

print $response->error_as_HTML;

}

}

$dbh -> disconnect();

=====================================================================================================

SENDING SMS VIA SMPP CONNECTION

The following code need the sendsmsclass.php file. I have pasted the code of sendsmsclass.php under the class. Also you can send flash sms via the same code.

=============================================================================================

File Name : sendsms.php

<?php

$i=0;

include('sendsmsclass.php');

//SMS Server Variable

$host="122.XXX.XXX.XXX";

$port=6095;

$system_id="TEST";

$password="testpwd";

$src = "TESTSMS"; // shortcode or text e.g. SD,5XXXX

//Connecting with SMSC

$message = "Hi I am SANDIP Testing SMPP Connection";

//Connecting with SMSC

$s = new smpp();

$s->debug=1;


// $host,$port,$system_id,$password

$s->open($host,$port,$system_id,$password);


//Waiting for 1 sec before establishing the connection

sleep(1);

$dst='919971114501'; //My mobile where the SMS will be sent

//Sending SMS

$s->send_long($src, $dst, $message);

//Closing SMSC connection

$s->close();

?>

File Name : sendsmsclass.php

<?php

class smpp {

var $socket=0;

var $seq=0;

var $debug=0;

var $data_coding=0;

var $timeout = 2;

//////////////////////////////////////////////////

function send_pdu($id,$data) {

// increment sequence

$this->seq +=1;

// PDU = PDU_header + PDU_content

$pdu = pack('NNNN', strlen($data)+16, $id, 0, $this->seq) . $data;

// send PDU

fputs($this->socket, $pdu);

//print "\nPDU SEND :".$pdu;

// Get response length

$data = fread($this->socket, 4);

if($data==false) die("\nSend PDU: Connection closed!");

$tmp = unpack('Nlength', $data);

$command_length = $tmp['length'];

if($command_length<12) return;

// Get response

$data = fread($this->socket, $command_length-4);

$pdu = unpack('Nid/Nstatus/Nseq', $data);

if($this->debug) print "\n< R PDU (id,status,seq): " .join(" ",$pdu) ;

return $pdu;

}

//////////////////////////////////////////////////

function open($host,$port,$system_id,$password) {

// Open the socket

$this->socket = fsockopen($host, $port, $errno, $errstr, $this->timeout);

if ($this->socket===false)

die("$errstr ($errno)<br />");

if (function_exists('stream_set_timeout'))

stream_set_timeout($this->socket, $this->timeout); // function exists for php4.3+

if($this->debug) print "\n> Connected" ;

$system_type='SANDIPTEST'; //This is provider by the operator

// Send Bind operation

$data = sprintf("%s\0%s\0", $system_id, $password); // system_id, password

$data .= sprintf("%s\0%c", $system_type,"", 0x34); // system_type, interface_version

$data .= sprintf("%c%c%s\0", 5, 0, ""); // addr_ton, addr_npi, address_range

$ret = $this->send_pdu(2, $data);

if($this->debug) print "\n> Bind done!" ;

return ($ret['status']==0);

}

//////////////////////////////////////////////////

function submit_sm($source_addr,$destintation_addr,$short_message,$optional='') {

//echo $destintation_addr;

$data = sprintf("%s\0", "CMT"); // service_type

$data .= sprintf("%c%c%s\0", 5,0,$source_addr); // source_addr_ton, source_addr_npi, source_addr

$data .= sprintf("%c%c%s\0", 1,1,$destintation_addr); // dest_addr_ton, dest_addr_npi, destintation_addr

$data .= sprintf("%c%c%c", 0,0,0); // esm_class, protocol_id, priority_flag

$data .= sprintf("%s\0%s\0", "",""); // schedule_delivery_time, validity_period

$data .= sprintf("%c%c", 0,0); // registered_delivery, replace_if_present_flag

$data .= sprintf("%c%c", $this->data_coding,0); // data_coding, sm_default_msg_id

$data .= sprintf("%c%s", strlen($short_message), $short_message); // sm_length, short_message

$data .= $optional;

$ret = $this->send_pdu(4, $data);

// print "HI".$ret;

return ($ret['status']==0);

}

//////////////////////////////////////////////////

function close() {

$ret = $this->send_pdu(6, "");

fclose($this->socket);

return true;

}

////////////////////////////////////////////////// if you send to send flash sms set $flash other then 0 /////////////////////////////////////////////////////

function send_long($source_addr,$destintation_addr,$short_message,$utf=0,$flash=0) {

if($utf)

$this->data_coding=0x08;

if($flash)

$this->data_coding=$this->data_coding | 0x10;

$size = strlen($short_message);

if($utf) $size+=20;

if ($size<160) { // Only one part :)

// echo $source_addr."======".$destintation_addr."======".$short_message;

$this->submit_sm($source_addr,$destintation_addr,$short_message);

//echo 'Successfull';

} else { // Multipart

$sar_msg_ref_num = rand(1,255);

$sar_total_segments = ceil(strlen($short_message)/130);

for($sar_segment_seqnum=1; $sar_segment_seqnum<=$sar_total_segments; $sar_segment_seqnum++) {

$part = substr($short_message, 0 ,130);

$short_message = substr($short_message, 130);

$optional = pack('nnn', 0x020C, 2, $sar_msg_ref_num);

$optional .= pack('nnc', 0x020E, 1, $sar_total_segments);

$optional .= pack('nnc', 0x020F, 1, $sar_segment_seqnum);

if ($this->submit_sm($source_addr,$destintation_addr,$part,$optional)===false)

return false;

}

}

return true;

}

}

?>

=============================================================================================