Wednesday, June 21, 2006

Email SMS

After some toiling, I set up my computer to send me a short cell phone text message containing the subject, sender, and timestamp of each email that passess my filters. The Perl code for parsing the emails is listed below; my code is based off of an example from A.P. Lawrence.

Input arrives on stdin from .qmail-sms and leaves on stdout to qmail-inject. One small problem with this setup is that it does not retain much header information. When I tried to handle the mail objectively using a few different Perl classes, I couldn't properly parse the incoming messages.

One alarming moment of the testing involved me receiving over sixty emails in a few seconds when I accidentally created an infinite loop between gmail and the sms script. I rebooted the server when I realized what was happening. I was telling my mom about it when I realized that gmail would reconnect to my server again after the reboot. I ran back to my room just in time; it had already sent another sixty emails before I could stop it again.

#!/usr/bin/perl
while (<>) {
$subject=$_ if /^Subject:/;
$from=$_ if ( /^From:/ and not $from);
$date=$_ if ( /^Date:/ and not $date);
}
chomp $from;
chomp $date;
chomp $subject;
$subject =~ s/'//;
$subject =~ s/"//;
exit 0 if $from =~ /DAEMON/;
exit 0 if $from =~ /notify/;
print STDOUT <<EOF;
To: 8102653161\@mobile.mycingular.com
$date
$subject

$from
EOF