SMS Alerts of Important E-mails
ZOIS Technical Note TN-2007-12-01.
Author and Audience
This TN will be of interest to people who receive
important e-mails, but may not be immediately equipped or in a position
to process them. A reasonable competence in UNIX based operating
systems and a familiarity with the Perl scripting language are assumed.
Written by Martin Sullivan[au], ZOIS
Limited, Cockermouth.
Abstract
Scripts are presented, written largely in Perl, which allow an SMS
to be sent when an important e-mail is received.
Introduction
The author has found e-mail invaluable and uses it frequently to connect to colleagues, friends and acquaintances. By its very nature, however the medium lacks immediacy. An e-mail must be examined on specialised equipment and sometimes retrieved from some remote storage. E-mail needs to be therefore monitored and a number of devices have been produced which allow this to be done with some mobility, such as the Blackberry[bb]. Monitoring such e-mail however still remains a bothersome chore, made worse by the need to filter the important from the unimportant.
The current system, presented here, represents the culmination of
an evolutionary process that allows the author to monitor e-mails
automatically and should one of sufficient import arrive send an SMS
alert to his ever-present mobile phone. When suitably alerted the
author then endeavours to retrieve, read and act upon the e-mail using
either a normal computer, a PDA or the mobile phone's e-mail client.
Materials and Platform
The system described here only works with O2 UK's Web-based SMS System. In order to use this an account needs to registered. That can be achieved by following the instructions on the initial pages of web-site[o2] as an O2 customer. Obviously should a similar system from another mobile provider be used (in the UK or elsewhere) then the following scripts will need to be modified.
The popular mail processing program procmail(1)[pm] is used to decide which e-mail message is important or not. The rules for this can be made quite complicated, but in the example below the SMS is sent should the sender be either in a small hand-maintained white-list or the message have the text 'SENDSMS' in the subject line.
Much of the rest is down to a single script written in Perl which
uses the LWP module[lw]. The script also uses
Mail::Internet to parse the input message for Subject lines are so
forth. Both it and LWP are available from CPAN[cp].
Method
There is a pivitol need to set up SMS Account on O2 web site[o2]. At time of writing you get 10 free ones per month, and you will probably need to purchase additional SMS, which can be done via a Credit Card.
Initially a CPAN Perl module (Net::SMS::02) was found which claimed to do the necessary interaction, but it appeared broken. The 'correct' course may have been to investigate and fix this module. It was found easier, however, to simply code an ordinary script to do what was required. The script was not too complicated the LWP module providing the necessary heavy-lift under-the-covers HTTP and cookie handling.
In order to decide what is important a system which performs checks, crude or otherwise on the inbound message is required. Here's an example from the authors procmailrc(5) file.
:0 c
* $^TO_${ME}
* $^From:.*${SMS}
| perl ${HOME}/procmail/sms.pl
# If it has 'SENDSMS' in the subject, but isn't already sent ...
:0 c
* Subject:.*SENDSMS
* $!^From:.*${SMS}
| perl ${HOME}/procmail/sms.pl
The variable SMS contains a '|' separated list of important
correspondents address, for example ...
SMS='billg@microsoft.com|stevej@apple.com'
The $ME variable contains a similar list of e-mail names that are associated with the author.
The above procmail script executes a Perl script, sms.pl. Use this for sms.pl. The standard Source Code Caveat apply:
Since the examples have been coded with illustration in mind they will not work as is. If you want to, you can do some further coding to make them workable. If you do so, then proceed to blow up that vital file, your company's most important database, or indeed the whole planet, then it's not our fault. Check out our views on Copyright on this stuff too.
#!/usr/bin/perl -w
# SMS - Send a text based on an inbound e-mail.
# This started as a test harness to try and authenticate to O2, and txt
# automatically. See also Net/SMS/O2.pm, which no longer works (2007-10-24).
# $Id: tn-2007-12-01.html,v 1.2 2011/06/13 18:32:31 sullivan Exp $
use strict;
use LWP::UserAgent;
use Mail::Internet;
#use LWP::Debug qw(+);
my $login = "https://zarkov.shop.o2.co.uk/login/mblogin";
my $sms = "http://sendtxt.o2.co.uk/sendtxt/action/compose";
my $logout =
"http://zarkov.shop.o2.co.uk/logout.html?dest=http://www.o2.co.uk/";
my $username = ""; # your username
my $password = ""; # your password
my $mobile = ""; # your mobile number
my $ua = LWP::UserAgent->new ();
my $mail = Mail::Internet->new (\*STDIN);
# Get the sender and subject of the e-mail and make a small message out
# of it.
my $head = $mail->head;
my $subject = $head->get ("Subject");
chomp $subject;
my $from = $head->get ("From");
chomp $from;
my $message = ($from || "") . ": " . ($subject || "");
push @{ $ua->requests_redirectable }, 'POST';
# O2 depends on redirects after POST
$ua->cookie_jar({}); # and lots of dumb to dubious cookie stuff
$ua->timeout (360); # double it
my $doc;
$doc = $ua->get ($sms) or die $doc->status_line;
# The above provokes a login page, which is filled in with the
# following.
$doc = $ua->post ($login, {
'ACTION' => "LOGIN",
'dest' => $sms,
'fu' => "http://www.o2.co.uk/logerror?sendTo=" . $sms,
'USERNAME' => $username,
'PASSWORD' => $password
}) or die $doc->status_line;
# Then get the real page (setting a bunch of cookies) ...
$doc = $ua->get ($sms) or die $doc->status_line;
# What have we got left ...
my $free = 0;
my $paid = 0;
$doc->content =~ /Use free text. You have (\d+) texts remaining/;
$free = $1;
$doc->content =~ /Use paid text. You have (\d+) texts remaining/;
$paid = $1;
# Try and use 'free' rather than 'paid'. There's a maximum size which is
# altered in real-time on the website using Javascript and describes the
# amount of message left. The different maximum sizes appears to be due
# to a small ad being being stuck on the end of the 'free' one. The amount
# of message left is used in internal calculations about the size of the
# message, so this is passed in too. In spite of these calculations, the
# 'free' SMSs can only be 110 chars long too, or then the system uses
# multiple messages (this is thus the practical limit).
my $paytype;
my $maxchars;
if ($free > 0) {
$paytype = "free";
$maxchars = 430;
} elsif ($free == 0 && $paid > 0) {
$paytype = "paid";
$maxchars = 466;
} else {
die "No texts left or paid/free parsing error";
} # else
$message = substr ($message, 0, 110);
$doc = $ua->post ($sms, {
'compose.to' => $mobile,
'compose.message' => $message,
'chars' => "" . ($maxchars - length ($message)),
'total' => "1",
'compose.paymentType' => $paytype,
'execute' => "send"
}) or die $doc->status_line;
if ($doc->content =~ "has been sent") {
print "SMS sent\n";
} else {
die "Message not sent for some reason:" . $doc->content;
} # else
$doc = $ua->get ($logout) or die $doc->status_line;
0;
Discussion
This system was originally written in 2007 (hence the TN name) but these notes were written in 2010, so it can be considered stable. The system is heavily used, particularly in respect to e-mails about the authors health.
While stable, the caveats associated with effectively scraping a third-parties web-site apply. In effect, we are beholden to O2 to not change their interface. O2 allow 10 messages per month, and this limit is easily broken. It is therefore useful to purchase additional SMS messages and this can be done via a credit card. It should be noted that when done like this sending larger number of messages becomes expensive. A solution using one of a number of specialised agencies or a tethered mobile phone and a suitable tariff is then suggested.
A variation of the sms.pl script (above) can be used to check the number of messages left. The construction of this script is left as an exercise for the reader, but the author can be contacted for his version. If there is nothing left, free or paid-for, then the SMS is not sent.
SMS messages need to remain important (and therefore not
ignored). Their importance is obviously linked to the number that you
receive. To restate, the SMS delivery system relies on the
availability of the O2 web-site. Finally it is important to note that
SMS is a store-and-forward system and that it has no guaranteed
delivery. It is not uncommon for SMS messages to not be delivered for
days.
References
References found in this section, and in particular the HTML links were correct at time of writing (2010-01-10).
- [au]. Martin Sullivan:
- http://www.zois.co.uk/people/martin_sullivan
- [bb]. Blackberry:
- http://www.blackberry.com
- [o2]. O2 SMS Interface. Registration required:
- http://sendtxt.o2.co.uk/sendtxt/action/compose
- [pm]. Procmail:
- http://www.procmail.org
- [lm]. Perl's LWP module:
- http://search.cpan.org/perldoc?LWP
- [cp]. CPAN:
- http://www.cpan.org
~Z~