News
Sep 17, 2011

Site layout and content has been updated. All sections except the Photogallery now use the new layout


scripts.suramya.com -> Email2SMS

Email2SMS

Introduction

Email2SMS is a Perl Script that uses gnokii (among other things) to convert an incoming email sent to a specific email address to an outbound SMS.

This script was created as part of my article in the Linux Gazette(LG) Issue #164: Sending and Receiving SMS from your Linux Computer. The article does a decent job on how to setup and use the script so I am just going to reproduce part of it here instead of writing another guide. The complete article is available on the LG site and in the Tutorials section on my site.

- Suramya


Sending and Receiving SMS from your Linux Computer

What is SMS? SMS stands for Short Message Service and is used to send text messages from one GSM enabled cell phone to another. But sometimes you might want to send an SMS message using your computer. Now there are three ways that you can send an SMS from a computer:

  1. Use a SMS gateway service that allows your computer to connect to it using an API and send messages. There are a lot of these services available on the web but most are not free and require subscriptions
  2. Use a cell-phone/GPRS Modem connected to the computer.
  3. Some service providers allow you to send SMS to a number by sending an email to a specifically formated email address.

I choose to use the second option because my provider doesn't support the last option and I think its cheaper than option one. All you need for this to work is an extra phone and a cheap phone plan that allows you to send and receive SMS's. In my case I had an old Sony Ericsson T610 lying in my desk gathering dust so I got a pre-paid SIM card and I was ready to proceed.

Pre-Installation Steps

The first thing you need is to connect your cellphone to your computer. I recommend using Bluetooth for it because that way there is one less cable you have to worry about on your desk but you can use a USB cable to connect if your phone doesn't support Bluetooth.

If you are using Bluetooth to connect then you will need to Pair the phone and the computer and configure the phone to allow your computer to connect to it without authorization. The steps for this differ from phone to phone so you will have to check the manual for the exact steps to follow.

Once we have the phone configured to auto-accept connections we are ready to proceed. Now in order to allow your computer to talk to your phone you will have to install a modem driver for mobile phones on your computer called gnokii. The steps for this are explained below

Installing gnokii

To install gnokii on a Debian system run the following command as root:

apt-get install gnokii

This will download and install gnokii on your computer. Most of the other distributions should also have an installer package available for download but in-case your distribution doesn't then you can always compile and install it from source.

To install from source you will need to download the latest version of gnokii from the project website( http://www.gnokii.org/download/gnokii/).

Once we have the latest version of the source we can install it on our system. To do that we must first uncompress the archive so issue the following command:

tar -zxf gnokii-0.6.27.tar.gz

Once tar finishes uncompressing the archive change to the gnokii directory using the following command:

cd gnokii-0.6.27/

Now if you are connecting your phone to your computer using Bluetooth you will need to install the libbluetooth-dev library first by issuing the following command as root:

apt-get install libbluetooth-dev

Once the library is installed you can configure the gnokii installer using the following command:

./configure --with-bluetooth=/usr/include/bluetooth

If you are not using Bluetooth then you will have to use the following command instead:

./configure 

Once the configuration is complete and you donít see any errors compile gnokii by running the following command:

make

If the compile finishes without errors install gnokii by running the following command as root:

make install

After gnokii finishes installing you will have to configure it before you can start using it.

Configuring gnokii

A sample configuration file for gnokii is provided in the source file under the Docs/sample/ subdirectory in the directory where you extracted the gnokii source. If you installed gnokii using apt-get then the sample file is installed at /etc/gnokiirc

The easiest way to configure gnokii is to modify the sample configuration file so we start by copying the sample configuration to your home directory using the following command:

cp gnokii-0.6.27/Docs/sample/gnokiirc ~/.gnokiirc

and open it in your favorite text editor for editing. The first thing you need to do is tell gnokii what phone you are using by setting the 'model' variable according to the matrix below:

  • Set model = series40, if you have Nokia non-Symbian Series60 phone
  • Set model = gnapplet, if you have Nokia Symbian Series60 prior to 3rd Edition phone
  • Set model = AT, for all other phones.

In my case I set it to AT.

Next, you need to tell gnokii how your phone is connected to the computer. For this you will have to change the 'connection', 'port' and 'rfcomm_channel' variables.

If you are using Bluetooth to connect then set the connection variable to 'bluetooth' and the port variable to your phone's Bluetooth address as shown below:

connection = bluetooth
port = 00:1e:3a:7e:a5:1f

According to the manual the 'rfcomm_channel' can be omitted as its auto-detected but I had to explicitly set it to get gnokii to connect.

Most of the phones use rfcomm_channel=1 but for my Nokia N95 8GB I had to set it to 2 while the Sony Ericsson T610 used rfcomm_channel=1. If you are not sure then you can use 'sdptool search SP' or 'sdptool search DUN' or 'sdptool browse' to find the right channel for your phone.

If you are using USB to connect then the connection name depends on the type of cable you are using and the phone model. Some values you can try are 'connection = serial' to use the AT protocol and 'connection = dku2libusb' to use the FBUS protocol (if supported by cable and phone).

The port name also depends on the type of the cable: some cables create a /dev/ttyACM0, others create a /dev/ttyUSB0. When using connection = dku2libusb, port = 1 chooses the first phone, port = 2 the second, and so on. See /var/log/messages to identify the port the phone connects on.

After you make the changes and save the file you are ready to test the connection.

Testing gnokii connection to phone

At a command prompt run the following command:

gnokii --identify

If you have configured everything correctly then you will see an output listing your phone model like the following:

suramya@Wyrm:~$ gnokii --identify
GNOKII Version 0.6.9
IMEI         : 35954XXXXXXXXXX
Manufacturer : Nokia
Model        : Nokia N95 8GB
Revision     : V06wk41v46.1

If you don't see this output then gnokii wasn't able to connect to your phone. Make sure that your phone is accepting bluetooth connections, you have the correct model specified and that you are trying to connect to the correct address using the correct protocol.

If you still are not able to connect then you should enable the debug mode in gnokii by setting 'debug = on' (Without the quotes) in ~/.gnokiirc. Looking at the debug output will let you identify the problem and rectify it.

Sending an SMS from your computer

Once you have configured gnokii correctly and get the above output you are ready to send SMS's from your computer. The command to send an SMS is:

gnokii --sendsms destination_number

This command will take the SMS content from the standard input. For example the transcript of sending a SMS to 555-123-4567 will look something like this:

suramya@Wyrm:~$ gnokii --sendsms  5551234567
GNOKII Version 0.6.9
Please enter SMS text.
End your input with <cr><control-D>:This is a test message
Send succeeded!
suramya@Wyrm:~$

gnokii has loads of other functionality. Use gnokii --help to get a list of parameters and functionality.

Now that you have the connection working you are only limited by your imagination as to how you use this. I built a gateway that allows me to send an SMS by sending an email to a specific email address. This program is described in more detail below.

Sending SMS using specially formated Emails

To send an SMS when I receive a specifically formated email I needed to write a program that checks for new emails and then processes them. I used Perl for this as its great for text processing and I had already written part of the code for another project (sending/receiving IM's as email.)

The first thing I needed was a new email account that I could access using POP3. Since I have my own domain I logged on to my domains admin section and created a new email address but you can use gmail if you want.

In my code I assume that the phone # to which I am sending an SMS to is in the subject of the email sent and the body of the SMS is the actual body of the email.

The code to connect and download the email is simple.

Basically we connect to the email server, download the list of new emails and then download each email in a loop, process them and then delete the message.

All the processing work is done in the ProcessEmail(). In this function we first extract the 'From' name from the email (this is appended to the SMS so that the recipient knows who sent it).

Once we have the 'From' field we extract the subject from the email and make sure its a phone number (This number can be prefixed with a + for country codes). If its not a number then we ignore the email and abort.

Finally we extract the body from the email. This becomes the body of the SMS message

Now we have all the required components for sending the SMS: Destination Number, SMS Body and the From address so we are ready to send the SMS. However since we can only send a max of 160 chars in one message we need to make sure that the SMS body is broken down into 160 char chunks before we can send the SMS

Now that you have your phone connected to your computer you can have your computer SMS you when a certain criteria like a particular incoming email, or when you have an important appointment coming up. You can have it wait for a message and then initiate a call to a specific number and play a wave file to the phone. All you are limited by now is your imagination and the capabilities of your phone.

Download Link

Download Email2SMS Ver 1.0 from here.

Change Log

Ver 1.0: (1st July 2009):

  • Initial release of the Script
Updates and corrections

Now that you have your phone connected to your computer you can have your computer SMS you when a certain criteria like a particular incoming email, or when you have an important appointment coming up. You can have it wait for a message and then initiate a call to a specific number and play a wave file to the phone. All you are limited by now is your imagination and the capabilities of your phone.

I am interested in hearing about any projects that you might build based on this article so if you have an interesting project idea do contact me.

- Suramya
  24th Mar 2010