Archive for April, 2006

Laziness-proof eMail backend script

Saturday, April 22nd, 2006

This started yesterday at work and was finished this morning after I passed out dead-tired last night.

I wrote a backend script for handling the emails that come to “all AT pranav NOSPACE sharma DOT com”

Now why did I do this?

Many emails were going around to that address and since some people simply hit “Reply All”, the authors of previous emails got the reply twice. This was annoying!!!

So, without going into much detail, I redesigned the whole process of how these emails got relayed to each person. Now you can click either “Reply” or “Reply All”, it won’t matter. This thing is laziness-proof (although I want to say idiot-proof).

So without further delay, here is my script which was shamelessly inspired from MEOW (ebackend.com). If you want to use this, please remove the ‘\’ before each ‘<' and '>‘. Also, notice the email addresses have been written so that robots cannot read it on this post. For the actual script, you must use actual email addresses with the ‘@’ preceeded by a ‘\’ like this: ‘\@’

#!/usr/bin/perl -w

######################################################################
## The following code has been adapted from MEOW (www.ebackend.com) ##
######################################################################

BEGIN {

$::INC[@::INC] = “/home/pranavs/public_html/all AT pranav NOSPACE sharma DOT com/Lib”;

require Mail::POP3Client;
require XML::Simple;
}

print “Content-type: text/html\n\n”;

# Read login information

my $xml_config = ‘/home/pranavs/public_html/all AT pranav NOSPACE sharma DOT com/config.xml’; # XML Configuration

my $config = &ReadConfig($xml_config);
if ( !$config ) {
print “\nConfiguration file Invalid / Not Present\n “;
exit;
}

# Get mails from the mailbox into %NewItems
# $new_items stores count
my $new_items = &ReadMailBox($config, \%NewItems);

if ( !$new_items) {
print “\nNo new messages for all AT pranav NOSPACE sharma DOT com”;
exit;
} else {
&SendMail(\%NewItems);
}

######################################################################
####################### Helper Functions #############################
######################################################################

sub ReadConfig {

my ($config) = @_;

$config = XML::Simple::XMLin(“$config”) ;

return $config;
}

sub ReadMailBox {

my ($config_hash, $new_item_hash) = @_;
my $msg_count;

# Connect to a POP Mailbox using POP3Client library
$Mail = new Mail::POP3Client( USER =\> “$$config_hash{‘mail’}{‘user’}”,
PASSWORD =\> “$$config_hash{‘mail’}{‘pass’}”,
HOST =\> “$$config_hash{‘mail’}{‘host’}”,
);

$$config_hash{‘mail’}{‘newitems’} = $Mail-\>Count();

if ( $$config_hash{‘mail’}{‘newitems’} \< 1 ) {
return 0;
}

for( $msg_count = 1; $msg_count \<= $$config_hash{'mail'}{'newitems'}; $msg_count++ ) {

foreach( $Mail-\>Head( $msg_count ) ) {

if ( $_ =~ m/^((From|Subject)):(.*?)$/i ) {
$header = lc($1);
$$new_item_hash{$msg_count}{$header} = $3;
}
}

$$new_item_hash{$msg_count}{‘body’} = $Mail-\>Body( $msg_count );

#Adding a footer to each email with unsibscribe instructions
if ($$new_item_hash{$msg_count}{‘body’} =~ m/\<\/body\>\s*\<\/html\>/i) {
$$new_item_hash{$msg_count}{‘body’} =~ s/\<\/body\>\s*\<\/html\>/\\\
To stop receiving these emails, send an email to unsubscribe AT pranav NOSPACE sharma DOT com.\To see a list of subscribed members, send an email to whois AT pranav NOSPACE sharma DOT com.\<\/body\>\<\/html\>/i ;
} else {
$$new_item_hash{$msg_count}{‘body’} .=”\n\n——————————————————-\nTo stop receiving these emails, send an email to unsubscribe AT pranav NOSPACE sharma DOT com.\nTo see a list of subscribed members, send an email to whois AT pranav NOSPACE sharma DOT com”;
}

if( $$new_item_hash{$msg_count}{‘from’} =~ m/(.+)\s*\<([\+\w\.\-]+\@[\w\.\-]+\.[a-z][a-z]+)\>/i ) {
$$new_item_hash{$msg_count}{‘from’} = $1;
}

$$new_item_hash{$msg_count}{‘valid’} = 0;
$Mail-\>Delete( $msg_count);
}

$Mail-\>Close();

return $$config_hash{‘mail’}{‘newitems’};
}

sub SendMail {

my ($newItems_List ) = @_;

foreach $msg_count ( keys %$newItems_List ) {

unless(open (MAIL, “|/usr/sbin/sendmail -t”)) {
print “error.\n”;
warn “Error starting sendmail: $!”;
}
else{
if($$newItems_List{$msg_count}{‘body’} =~ m/\s+–(.*[=_\.0-9a-z]+)–/i ) {
my $boundary = $1;
print MAIL “Content-Type: multipart/alternative;\n”;
print MAIL ” boundary=\”$1\”\n”;
print MAIL “MIME-Version: 1.0\n”;
}
print MAIL “From: $$newItems_List{$msg_count}{‘from’}\\n”;
print MAIL “BCC: all DO NOT REPLY to this address AT pranav NOSPACE sharma DOT com\n”;
print MAIL “Subject: $$newItems_List{$msg_count}{‘subject’}\n”;
print MAIL “\n$$newItems_List{$msg_count}{‘body’}”;
close(MAIL) || warn “Error closing mail: $!”;
print “Mail sent.\n”;
}

}
}

Kiss Kiss Bang Bangalore

Tuesday, April 11th, 2006

Those of you who did not check out the latest Simpson’s episode titled “Kiss Kiss Bang Bangalore”, missed out on a great episode.

Synopsis from TV.com:
At the power plant a movie is shown where the workers endorse outsourcing and Mr. Burns announces that he is closing the plant and moving the work to India. Homer is the only union employee left on the payroll and he is sent to India to train the replacement workers, when he succeeds beyond expectations, he is given total control of the plant and declares himself a god. Meanwhile, Selma & Patty take Bart and Lisa to their MacGyver convention. When they discover Richard Dean Anderson doesn’t like MacGyver and much as they do, they kidnap him in retaliation. Anderson escapes only to enjoy the thrill and he turns himself back in so that he can escape again. When it happens over and over again, soon Patty and Selma are the ones who need a means of escape.

Check out a clip from the episode that I recorded:
You Ignorant American.wmv (1 MB)

Windows Media Player 10 required to play the above video.

Due to copyright protection laws, I cannot post the entire episode but feel free to contact me and we can arrange something.