Wednesday, December 06, 2006

Setting up an SMS gateway

I am writing this little howto for setting up an email2sms gateway which basically sends a mail to your mobile , like any alerts etc.

Hardware/software requirements are:

1) A GSM modem (or a phone which can act as one). The key point is it should be able to talk to Linux as a modem. A lot of the modern CDMA/GSM phones have modems that need proprietary drivers to access the modem. These generally are not available for Linux.Choose one with support for linux.
Put your SIM into the modem, connect the serial cable to a serial-to-USB converter and connect to the system via USB port.( Nowadays I've seen the modems coming directly with a USB interface).

2) Linux , we used FC 5 since the modem drivers had some issues on RHEL.

3) The SMS software. We use here smstools ( http://smstools.meinemullemaus.de/)

Download the tar.gz file and do the normal installation - unzip , make , make install.

Go to the configuration file /etc/smsd.conf . Configure as follows

[root@smsgw]# cat /etc/smsd.conf
# Example smsd.conf. Read the manual for a description

devices = GSM1 # Put in any name here for the modem to be identified.
logfile = /var/log/smsd.log # Set the log file here so that you can use it for debugging
loglevel = 7 # Check out the http://smstools.meinemullemaus.de/configure.html for details


outgoing = /var/spool/sms/outgoing # Define directory where the mails will come
checked = /var/spool/sms/checked
failed = /var/spool/sms/failed # Failed sms , for reasons like say - nil balance in SIM
incoming = /var/spool/sms/incoming # Define directory where incoming sms will be stored
sent = /var/spool/sms/sent # Mails sent out as sms

[GSM1] # Define each modem you named above , eg GSM1 , GSM2 etc.
device = /dev/ttyUSB0 # Define the device name of the modem
incoming = yes

We had used a serial-to-USB converter to connect the modem to the system. The modem hence appears to the system as a SUB device. So we define the GSM modem device as /dev/ttyUSB0. The mails that come into the outgoing folder should have the To field as the mobile no. of the person to whom the sms should be sent to. For automatically setting it to change the names of the mail id in the To field of the mail to the phone no. you will have to configure a filter like procmail to automatically change this.

[root@odin sent]# cat /etc/procmailrc
SHELL=/bin/sh
LOGFILE=/var/log/Proctest.log
LOG="--- Logging for ${LOGNAME}, "
#First, a few environment variables are included.
#Troubleshooting:
VERBOSE=yes
LOGABSTRACT=all

:0
* ^TO_ .*@company.com
| /usr/local/bin/extract-headers.sh

This calls for execution of the script extract-headers.sh for all mails coming to @ company.com domain.
The extract-headers.sh is just a script to extract the required headers only from the mail body and replace it with the phone no.s of the persons concerned in the To field , since the smsd sends the message to the number given in the To field of the message. See http://smstools.meinemullemaus.de/fileformat.html for more details on the sms format to be followed.

[root@smsgw]# cat /usr/local/bin/extract-headers.sh
#!/bin/bash
TMPFILE=$(mktemp)
FILE=$(mktemp -p /var/spool/sms/outgoing/)
formail -k -XFrom: -XSubject: -XTo: >> $TMPFILE # From, To and Subject headers copied to TMPFILE
rcptad=$(grep ^To: $TMPFILE | gawk '{print $2}') # search To field, output only mail-id part
perl -pi -e 's/To.*/To: 91999999999/' $TMPFILE;; # Replace mail id with mobile no.
FROM=$(grep From: $TMPFILE |gawk -F":" '{print $2}') # Get the From machine name
echo "from $FROM" >> $TMPFILE # Push the line "from sender-name" to the TMPFILE
cp $TMPFILE $FILE # Move the contents of TMPFILE to FILE
rm $TMPFILE # Delete TMPFILE



END of story. Start smsd in your system. put some mail in the /var/spool/outgoing folder , pay your mobile bill ;) and pray for the sms alerts to come :).
Contact me for any doubts.
___________________________________________________________________________

--
Thanks and Regards
Nabeel Moidu
Red Hat Inc.
Pune, India

If we don't believe in freedom of expression for people we despise, we don't believe in it at all. Noam Chomsky

3 comments:

Anonymous said...

A good howto thanks bro

Dinesh Shah said...

H!

Can you tell me which mobile phone/cable you have used and which service provider you are using for sending and receiving SMS?

TIA

Aditya Kaler said...
This comment has been removed by a blog administrator.