SMS Tools is one opensource of SMS engine tools. Running on Linux Operating System, using bash command to send message, and restore sms to text file format on receiving SMS. SMSTools can downloaded from the site: http://smstools.meinemullemaus.de/smstools-2.2.20.tar.gz
Installation
1. Download the SMS Tools
wget http://smstools.meinemullemaus.de/smstools-2.2.20.tar.gz
2. Extract the tarball/package
tar -xzvf SMSTools
3. [Root Mode On] enter the SMSTools directory
cd SMSTools
4. Install it
make install
5. Configure it.
The configuration of SMSTools can find at /etc/smsd.conf. This file can be edited by Linux Text Editor such as : vi, mcedit, ee, pico, nano. The simple configuration will store such as:
# Example smsd.conf. Read the manual for a description
devices = GSM1
logfile = /var/log/smsd.log
loglevel = 7
[GSM1]
device = /dev/ttyS0
incoming = yes
#pin = 1111
Which line will be replaced? Before configuring this file, we must know the device identified. For Example, if we use Itegno GSM Modem, by added libmm library as additional driver, after the device was plugged, so, we can check with tail -f /var/log/message, and we can see which USB port the device was attached. For example: /dev/ttyUSB0.
So you can replace this line:
device = /dev/ttyS0
with
device = /dev/ttyUSB0
Pin Option can be use (uncomment) if the used SIM Card are locked by pin
You can add the baudrate line such as
baudrate = 115200
bautrate is the working frequency of the device to check any changes at SMS Incoming or Sending.
# Example smsd.conf. Read the manual for a description
devices = GSM1
logfile = /var/log/smsd.log
loglevel = 7
[GSM1]
device = /dev/ttyusB0
incoming = yes
#pin = 1111
baudrate = 115200
6. Run it
For Redhat Family (Centos/Fedora/Redhat) please use:
/etc/init.d/sms start
For Slackware please use
nohup /usr/local/bin/smsd start &
7. Check and troubleshoot
To check and troubleshoot, we suggest you to open other terminal, and open log file that writen at configuration file, with tail -f
tail -f /var/log/smsd.log
8. Sending and receiving messages
To sending a message you can use this command:
sendsms no_phone message
for example:
sendsms 6281328857693 example to sending sms with SMS Gateway
Please check at log, what happened.
We can see any messages are coming from the log, and you can see the messages from
/var/spool/sms/incoming/*random-name-file
please using cat to view the file.
What should we do to make it simple in one SMS manager system?
Sending Message
It very simple to code it to a PHP code, for example
system("/usr/local/bin/sendsms $nophone ‘$message’");
For Example with simple example
This is a file that named : post.htm
<form method=’post’ action=’postsms.php’>
<input name="nophone" type="text" value="" /><br />
<input name="message" type="text" value="" /><br />
<input type="submit" value="POST">
</form>
Using postsms.php
<?php
mysql_connect(’127.0.0.1′, smsuser, smspass);
mysql_select_db(’sms’);
$message=$_POST[’message’];
$nophone=$_POST[’nophone’];
system("/usr/local/bin/sendsms $nophone ‘$message’ > /tmp/anysms.log &");
//for example there is a table outbox, contains : id(autoincrement), nophone, message, sendstatus, senttime
$query="INSERT INTO outbox VALUES('’,'$nophone’,'$message’,’sent’, ‘’)";
mysql_query($query);
?>
This example with advanced example
This is a file that named : sms.htm
<form method=’post’ action=’postsms.php’>
<input name="nophone" type="text" value="" /><br />
<input name="message" type="text" value="" /><br />
<input type="submit" value="POST">
</form>
Using postsms.php
<?php
mysql_connect(’127.0.0.1′, smsuser, smspass);
mysql_select_db(’sms’);
$message=$_POST[’message’];
$nophone=$_POST[’nophone’];
//for example there is a table outbox, contains : id(autoincrement), nophone, message, sendstatus, senttime
$query="INSERT INTO outbox VALUES('’,'$nophone’,'$message’,'notsend’, ‘’)";
mysql_query($query);
?>
And we can make a file that we can use it just for sending sms job, and please insert it to crontab file.
sending.php
<?php
mysql_connect(’127.0.0.1′, smsuser, smspass);
mysql_select_db(’sms’);
$query = "SELECT * FROM `outbox` WHERE `sendstatus`=’notsend’";
//for example there is a table outbox, contains : id(autoincrement), nophone, message, sendstatus, senttime
$execute = mysql_query($query);
while ($result[0] = mysql_fetch_array($execute){
system("/usr/local/bin/sendsms $msisdn ‘$message’ > /tmp/anysms.log &");
$qupdate = "UPDATE `outbox` SET `sendstatus`=’sent’ and senttime=now() WHERE `id`=’$result[0]’";
}
?>
crontab -e
*/2 * * * * /var/www/html/core/sending.php
RECEIVING With SMSTools
Now, please check at /var/spool/sms/incoming/ and take one file with cat command
For example
cat /var/spool/sms/infoming/286AHUHH3435645
You will get this format
From: 6285242396221
From_SMSC: 6281100000
Sent: 09-03-13 07:45:47
Received: 09-03-21 05:53:06
Subject: GSM1
Alphabet: ISO
UDH-DATA: 05 00 03 09 02 02
UDH: true
TEST abcde aa7890bb
Now, how to make this file will be restored in a MySQL database? The solution is : parsing. Please check at this file
read.php
<?php
include ‘dbcon.php’;
//dbconnection are included in this file
include ‘readir.php’;
$file = fopen(’/var/spool/sms/incoming/ct’,"r");
while(! feof($file))
{
$fget=fgets($file);
$o=explode(": ",$fget);
if((substr($o[0],0,4)=="From") && (substr($o[0],0,9) != "From_SMSC")){
$FROM=$o[1];
}
elseif(substr($o[0],0,9)=="From_SMSC"){
$SMSC=$o[1];
}
elseif(substr($o[0],0,4)=="Sent"){
$SENT=$o[1];
}
elseif(substr($o[0],0,8)=="Received"){
$RECEIVED=$o[1];
}
elseif(substr($o[0],0,7)=="Subject"){
$SUBJECT=$o[1];
}
elseif(substr($o[0],0,8)=="Alpabeth"){
$ALPH=$o[1];
}
elseif(substr($o[0],0,8)=="UDH-DATA"){
$UDHD=$o[1];
}
elseif(substr($o[0],0,8)=="UDH"){
$UDH=$o[1];
}
elseif((substr($o[0],0,1)!=" ") && (substr($o[0],0,4)!="From") && (substr($o[0],0,9)!="From_SMSC") && (substr($o[0],0,4)!="Sent") && (substr($o[0],0,8)!="Received") && (substr($o[0],0,7)!="Subject") && (substr($o[0],0,8)!="Alpabeth") && (substr($o[0],0,8)!="UDH-DATA") && (substr($o[0],0,3)!="UDH")){
$MESSAGE=$o[0];
}
}
fclose($file);
$qinsert="insert into inbox values (’$FROM’,'$SMSC’,'$SENT’,'$RECEIVED’,'$SUBJECT’,'$ALPABETH’,'$UDHD’,'$UDH’,'$MESSAGE’,'U’)";
mysql_query($qinsert);
?>
But this file are not work properly without other file support. This mechanism of this file are:
1. Just read /var/spool/sms/incoming/ct
2. The /var/spool/sms/incoming/ct are generated by the bash shell file that have some job to : move file from incoming to ct file, and to archive file. So with this mechanism, the incoming directory will always empty, and ct is the updated file. The old content will be replaced with the newer.
3. When the ct is contain a active content, this file will be activated.
For additional information, the directories structure in my example are:
/var/www/html/engine/read.php
/var/www/html/engine/getsms.sh
/var/www/html/core/post.htm
/var/www/html/core/postsms.php
/var/www/html/core/sending.php
#getsms.sh
#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
# set me
FILES=/var/spool/sms/incoming/*
FILES1=/var/spool/sms/container/
FILES2=/var/spool/sms/archive/
for f in $FILES
do
echo "$f"
cp "$f" $FILES2
mv "$f" $FILES1/ct
/usr/local/bin/php /var/www/html/engine/read.php
done
# restore $IFS
IFS=$SAVEIFS
Please make this file in your crontab.
* * * * * /var/www/html/engine/getsms.sh