Becoming a rememberer - an aid to remembering annually recurring events, such as, birthdays.
July 17th, 2004 admin
Here is a miraculous solution to the age old problem of forgetting birthdays and anniversaries. A perl script that can send you email every morning about what’s coming up for the next week, so not only do you remember, but you have enough time to set up something special … like a surprise birthday cake!
OK enough of the introduction. Essentially it is a simple perl script that I run on my Unix machine. At it has already proven its worth. So this is for those of you who are looking for a similar tool.
#!/usr/bin/perl
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#Written by: Anil Krishna
#On: July 10th 2004
#Purpose: This perl script sends an email to a given address, with a listing of events that are going to happen in the nesr future. The “nearness” is described by a variable called $warnZone in the script.
#Dependency: This program needs a dateslist.txt file to contain the dates and corresponding events.
#The dateslist.txt file should contain the dates in the following format for this perl script to work right.
#month_in_digits date_in_digits String Describing the event
#example:
#1 1 New Year’s
#8 15 India’s Independence
#9 1 bright’s Birthday; email address: bright@tubelight.com
#Usage: To use this script, create the dateslist.txt file. You can add to it as appropriate. Set the $from and $to variable to point to appropriate addresses. Set the $warnZone to any other number if you want. Right now it is set to 7, which means the program will warn you about events happening in the next 7 days (including today). And you would likely want to run this program with some regularity. I run this program once a day. The way I automate that is by creating a crontab entry in a Unix machine where I store these files. Crontab is a table where you can specify time periods for autoamtically having the system run executable programs such as this one. The way it typically works is do a “crontab -e” call on your Unix machine. It will open up a file for you to edit (using the system’s default text editor. Type in the following command to have it run the program every morning at 6 AM. “0 6 * * * ~/date.pl”. What this is saying is at 0 minutes, 6 hours, every day of the month, every month, every day of the week, run ~/date.pl. You would have to change the path to this perl script according to where it is located on your Unix machine. You could also choose to run this script only every Thursday midnight, by doing this to the crontab, “0 0 * * 4 path/to/your/foleder/file.pl”. By the way, just as “crontab -e” lets you edit the crontab, “crontab -l” lists the current crontab entries.
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#find today’s day of month (mday) and month (mon)
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime (time);
#day of year = yday+1 (since the count starts from 0 in perl)
$today_doy = $yday+1;
#array defining days in months, and variable defining num days in year
if($year%4 == 0){
@dpm=(31,29,31,30,31,30,31,31,30,31,30,31);
$daysThisYear = 366;
}
else{
@dpm=(31,28,31,30,31,30,31,31,30,31,30,31);
$daysThisYear = 365;
}
#read the entries in the table of dates and see if there is any that is close. Close is defined as being within nearEnough (defualt 7) days.
$warnZone = 7;
open(DATES,”dateslist.txt”) or die “dateslist.txt could not be opened \n”;
while ($line = )
{
($event_month, $event_dateInMonth, @event) = split(” “, $line);
$event_doy=0;
for($i=1;$i<=$event_month;$i++){
if($i<$event_month){
$event_doy += $dpm[$i-1];
}
else{
$event_doy += $event_dateInMonth;
}
}
$proximity = $event_doy-$today_doy;
#if the event is in the same year as today then proximity is positive, and its being withing the warnZone is good enough.
#if the event is in the next year, then proximity return a negative value. daysThisYear + proximity indicate the real proximity of the event, and its being withing the warnZone is good enough.
if( ($proximity >= 0 && $proximity <= $warnZone) || ($proximity < 0 && $daysThisYear + $proximity <= $warnZone ) )
{
$content .= “@event on $event_month/$event_dateInMonth \n”;
}
}
#send email with the above collected content content.
use Net::SMTP;
$from = “sender\@source.com”;
$to = “receiver\@destination.com”;
$subject = “Important Upcoming Dates”;
$relay = “mailrelay.charlotte.servercompany.com”;
$smtp = Net::SMTP->new($relay) || die “Can’t open mail connection: $!”;
$smtp->mail($from);
$smtp->to($to);
$smtp->data();
$smtp->datasend(”To: $to\n”);
$smtp->datasend(”From: $from\n”);
$smtp->datasend(”Subject: $subject\n”);
$smtp->datasend(”\n”);
$smtp->datasend(”$content\n”);
$smtp->dataend();
$smtp->quit();
Posted in Tidbits | No Comments »