send E-mail headers to your cell phone

Cellmail -- Making use out of your free SMS messaging



I wrote Cellmail because I couldn't find anything like it on the Internet. Oh, I'm sure its out there, but after three days of looking I figured I could have written what I needed, three times over. I found some stuff that would send varying things to the phone, but not what I wanted.

All I wanted was something that would send an SMS message to my phone that had the format:

From: <my server>
To: <my cell number>
Subject: New Mail.
Data: <Original Sender> - <Original Subject>

Since (at least with most of the carriers in the US) it is free to receive messages, this costs me nothing. However, if my phone is beeping all night or beeping everytime I get a SPAM, it would be more of a pain than a help.

So to keep things tenable, I needed to limit the number of notifications that I actually receive -- certainly a subset of the total E-mails I get every day. The solution to limiting notifications lies within the code itself and within (no surprise) procmail.

Call it from procmail

Very simply, the .procmailrc file should have the call to cellmail after all calls to SpamAssassin and any other filtering you do. Duh.

You can also do some filtering directly within the procmail recipe:

:0hc
* !^FROM_MAILER
* !^From:.*apinger
* !^From:.*RHN
* !^Subject:.*\[rdiff-backup-users\]
| /home/bubba/cellmail

Probably the most important one here is the ^FROM_MAILER check which will make sure you don't get alerted when you get bounce messages, logrotate mails and the like. Unless, of course, you want to be notified when those mails come in.

Note the last line in the recipe. That pipe sends the header of the E-mail to the cellmail program. Of equal importance is the 0hc at the start of the recipe. That sends a copy of the header to the pipe. Its one of them thar "non-terminating" recipes.


Set Up Cellmail for Your Cell Phone

Simply look in the code below and you will find all the user settings in the first few lines of code, they are plainly marked.

The Code [Click to donwload or copy/paste from below]

 
#! /usr/bin/perl -w

##############################################################
#
#cellmail -- perl script to send summary messages to pagers
# cellmail (C) 2004, pettingers.org, All Rights reserved under GPL license:
# http://www.gnu.org/licenses/gpl.txt
#
#This will grab the headers from incoming E-mail fed via procmail
#or other mail pre-processor.  It will then format and send a
#message suitable for a text pager or SMS cell phone message.
#
#The following must be done first:
# 1. Install MIME::Lite package from CPAN. Short summary:
#     [user]$ perl -MCPAN -e shell
#     cpan> install MIME::Lite
#
# 2. Add a recipe such as the following in your .procmailrc file:
#     :0Ehc
#     * !^FROM_MAILER
#     | /home/user/cellmail
#
# I put this very last, after all filtering and spam checking
# (hence the 'E' option) although you can customize as required.
# Be sure to consult "man procmialrc" if you don't know the details.
#
# 3. Unless you shut your device off at night (or need to get
# alerts at odd hours) you'll want to limit the times when alerts
# are sent (note this has NO effect of mail delivery to your inbox).
# You can do this one of two ways:
#
# 3a.  Use the user-configurable options below to set an "on hour"
# and an "off hour".  If you need more granualarity than the top of
# the hour, see step 3b.  If you need 24-hour alerts, just comment
# out the timing section below near the top of the code (yes, I know,
# it should be a subroutine....)
#
# 3b.  If you need to define things down to the minute, make a
# cron job to pull this program in and out.  I make a secondary
# script with the following:
#     #! /usr/bin/perl
#     exit;
#
# I then save that as cellmail.out and save a copy of cellmail as
# cellmail.in
# Then, just make a cron job to copy the .out file over the top of
# cellmail at your bedtime.  In the morning, have another cron job
# copy cellman.in back to cellmail.  You could also write two versions
# of .procmailrc and do the same thing.  Or just delete cellmail during
# off hours and let procmail throw the error.
#
# 4. Set up the user defined variables to suit your needs
#
# 5. Don't blame me if it doesn't work ;-)
#

use strict;
use MIME::Lite;

#####################################
####### User Defined Variables ######
#
my $limit = 140; # Max characters for outbound message minus header
my $offtime = 22; # Hour to suspend messages
my $ontime = 7; # Hour to start allowing messages
my $fromaddr = 'new@yourdomain.org'; # Who the message will come from
my $celladdr = '1235551234@mobile.att.net'; # Pager or cell address (to)
my $subj = 'New Mail!'; # Subject for the outbound message

#####################################

undef $/;
my %hdrs;
my @loctime;
my ($infrom, $insubject, $data, $message, $msg, $now) = '';

#####    ###########   ##########
# Comment out this section to use a cron job or receive 24-hour alerts
@loctime = localtime;
$now = $loctime[2];
if ( ($offtime > $ontime) and ($now >= $offtime or $now < $ontime) ) {
   exit; }
if ( ($offtime < $ontime) and ($now >= $offtime and $now < $ontime) ) {
   exit; }
#
#####    ###########   #########

$message = <>;

$message =~ s/\n\s+/ /g;  # Fix lines that wrap
%hdrs   =  (UNIX_FROM => split /^(\S*?):\s*/m, $message);  # Hash the headers

chop ($infrom = $hdrs{"From"});  # Find out who its from and chop the \n
chop ($insubject = $hdrs{"Subject"});  # Get the subject and chop the \n

#   Need to check for From lines that don't have <sender@domain.com>
if ( $infrom =~ m/</ ) {
   $infrom = substr($infrom, 0, index($infrom, '<'))
   }

#   Package up the message with a max length of $limit
$data = substr(('From: ' . $infrom . ' [' . $insubject . ']'), 0, $limit);

#  Format the message to send with MIME::Lite package
$msg = MIME::Lite->new(
        From    =>$fromaddr,
        To      =>$celladdr,
        Subject =>$subj,
        Data    =>" -- $data"
        );
#   Send it best way possible (usually sendmail)
$msg->send;

# end

You know its simple when the comments take up more lines than the actual code. Geez.

Prerequisites




Copyright 2004 Pettingers.org

Vectors at

pettingers.org