Last month we talked about connecting your cellphone to your PC using software and Infrared (IR) Interface (Cellphone Meets the PC, Page 62, PCQuest, May 2003). This time, we’ll build a simple application that will send e-mail alerts to your cellphone via SMS. The software will keep track of all your incoming e-mail, and send an alert every time you receive one.
This can be a very useful application to alert you of all the important e-mail that you’re expecting.
While we’re at it, we also hope to introduce some intermediate Perl concepts. Perl is a text-processing language written by Larry Wall. A highly flexible interpreted language, it finds widespread use in CGI scripting to system administration. Though originally written in the Unix, it has been ported to Windows also. If you have already dabbled in Perl, then you can just breeze through the Perl part.
An e-mail alert system would look up your POP3 mailbox, scan and try to find a mail that matches your requirement. For example, when your boss (or your girlfriend) sends you a mail, you should get an SMS. Sounds simple? In fact, it is. We will use gammu for interacting with the cellphone and Perl for the program logic.
The first component of this system is a user database that contains information about each user, such as e-mail ID, password, cellphone number and, of course, the e-mail IDs for which alerts need to be generated when you receive mail from them. The remaining components include a cellphone and network connectivity to the mail server in question. The easiest database to handle is MS Access and that’s what we will use.
Making a database in MS Access is extremely easy. Create a new blank database, go to design view and add fields. You can
add more fields, if needed. Once the design of the table is done, it’s time to feed in the data. For example, consider the information in the table below.
Once its done, we need to create a data source name or DSN. For doing this, go to ODBC sources in the control panel and create a new system DSN. Use the MS Access driver and connect it to the database file you just created. Let’s name the
DSN, SMS.
Now that we are all set, let’s get started with Perl.
Installing Perl
We used the Active State Perl distribution for Windows. Since it’s not freely distributable, you’ll have to download it from
www.activestate.com. Also, we have given the full source for the Win32 version on this month’s
DVD.
To add pre-built components, Perl uses modules similar to Java classes and C/C++ libraries. We would need modules for interacting with the mail server and with the MS Access database. If you are using Active Perl, then it is extremely easy. With the Activeperl distribution, you get a Perl package manager or ppm. After installing Active Perl, you should get the ppm shortcut on your program menu. After executing you should get a ppm> prompt, which downloads the desired modules and installs them. We need the DBI, ODBC (for accessing the database) and Mail:POP3client (for talking to the mail server) modules.
Downloading and installing them is as simple as typing install
You can use the Open Perl IDE for editing and creating Perl scripts. We’ve given it on this month’s DVD. Its interface is pretty standard and does not warrant any explanation. A simple text editor should also suffice.
|
|||||||||||||||||
|
Building the Application
There are three parts to the application: accessing the database, accessing the mail server and accessing the
cellphone.
|
Accessing the Database
By using the DBI and ODBC module, you can access the database. The code snippet, which does the above, is as follows:
#connect to the DSN.
$dbh = DBI->connect(“dbi:ODBC:sms”, “user”, “password” ,
{ RaiseError => 1, AutoCommit => 0 });
#sql query.
$sth = $dbh->prepare(“SELECT * FROM userdata” );
$sth->execute();
To access the individual rows, use the following:
while ( @row = $sth->fetchrow_array ) {
# the array row has now individual elements .
}
Accessing the Mail Server
The Mail::POP3Client module lets you do that.
#create a connection.
$pop = new Mail::POP3 Client(USER => “test”,
PASSWORD => “password”,
HOST => “server.mail.com”,
DEBUG => 1);
Once the connection is created, you can access individual mail and their headers. For example, $pop->Count will return the number of mail present. For more information, see the module
documen- tation.
Accessing the Cellphone
To get SMS from your cellphone, use:
System (“Gammu —getsms 1 1 > sms.data” )
This is a standard system call, similar to what is found in C/C++. It saves the first SMS in your inbox folder to a file called
sms.data.
Sending SMS is similar. Use the following:
System(“gammu –sendsms TEXT 98****** < data.sms”)
This will send the content of data.sms to 98**** as SMS. Make sure that there are less then 153 characters for keeping the SMS size limit.
To see how these components are intertwined, see the code on the DVD. We’ve given an idea of how you can use Perl to build an application for your cellphone. It’s not a full-fledged application. Given the flexibility of Perl, there are numerous, and perhaps better, ways of doing the same thing. A few limitations of the system are that your cellphone’s SMS memory will get full quickly and that you have to save e-mail passwords in clear text in the database.
Ankit Khare