Laziness-proof eMail backend script
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: ‘\@’
######################################################################
## 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’}\
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”;
}
}
}
One Response to Laziness-proof eMail backend script
Pages
What I'm Doing...
- What if Apple Designed SharePoint?: http://t.co/HmHXDXUV 16 hrs ago
- More updates...
Archives
- December 2011
- August 2011
- June 2011
- April 2011
- March 2011
- September 2010
- August 2010
- July 2010
- May 2010
- February 2010
- January 2010
- November 2009
- March 2009
- January 2009
- April 2008
- March 2008
- October 2007
- September 2007
- August 2007
- July 2007
- May 2007
- March 2007
- February 2007
- December 2006
- October 2006
- September 2006
- August 2006
- June 2006
- May 2006
- April 2006
- March 2006
- December 2005
- September 2005
- August 2005
- June 2005
- April 2005
- March 2005
- February 2005
- November 2004
- October 2004
- September 2004
- August 2004
- July 2004
- June 2004
- May 2004
- March 2004
- September 2003
- August 2003
- April 2003
Categories
- Chromium OS
- Internet Explorer 7
- Kwizcom
- Miscellaneous
- Movies
- MS Office
- Music
- Night on the Town
- Office 2007
- Outlook 2007
- Pictures
- Products/Shopping
- Recovered Entries (01/19/2005)
- Restaurants/Food
- Sharepoint 2007 ( MOSS / WSS )
- Sharepoint 2010 (SPS / Foundation)
- Tech
- The Law
- TV
- Uncategorized
- Video Games
- Vista
- Visual Studio 2010
- VMWARE
- VMWARE Server 2.0
- Website
- Windows Live
- Work
- XP





lovin it. p dawg u continue to surprise
haha ….
nice job dude.