I like to know when important dates (like my family's birthdays, important anniversaries, etc.) are coming up, but I don't actively look at a calendar every day. I do read my email everyday, however, so I wrote this little script to send me email reminders of these important days. I'd suggest setting it on cron so it runs everyday.

So here's the code.

#!/usr/bin/perl use strict; use DBI; use Net::SMTP_auth; use XML::Simple; my $config_file = $ARGV[0] || "/etc/importantdates.xml"; my $xml = XMLin( $config_file, 'ForceArray' => ['days'] ) or die "what +?"; my $dbh = initialize_database( $xml ); my @days_warning = @{ $xml->{'config'}->{'reminders'}->{'days'} }; my $sel_str = q( SELECT d_desc, d_date, MOD( DAYOFYEAR( CONCAT( YEAR( CURDATE() ), DATE_FORMAT +( d_date, '-%m-%d' ) ) ) - DAYOFYEAR( CURDATE() ) + 365, 365 ) AS diff FROM dates HAVING diff <= ? ORDER BY diff ); my $sth = $dbh->prepare( $sel_str ); $sth->execute( max ( @days_warning ) ); while( my ( $d_desc, $d_date, $diff ) = $sth->fetchrow_array() ) { my $str = $d_desc; foreach ( @days_warning ) { if ( $diff == $_ ) { $str .= " in $diff day(s)."; send_email( $str, $xml ); last; } } } $sth->finish(); $dbh->disconnect(); ###### SUBS ###### sub max { my $max = $_[0]; foreach ( @_ ) { $max = $_ if ( $_ > $max ); } $max; } sub initialize_database { my $xml = shift; my $username = $xml->{'config'}->{'db'}->{'username'}; my $password = $xml->{'config'}->{'db'}->{'password'}; my $database = $xml->{'config'}->{'db'}->{'database'}; my $hostname = $xml->{'config'}->{'db'}->{'hostname'}; my $dsn = "DBI:mysql:$database:$hostname"; my $dbh = DBI->connect( $dsn, $username, $password ) or die "C +ould not create MySQL connection"; my $drh = DBI->install_driver("mysql"); $dbh } sub send_email { my $content = shift; my $xml = shift; my $hostname = $xml->{'config'}->{'smtp'}->{'hostname'}; my $username = $xml->{'config'}->{'smtp'}->{'username'}; my $password = $xml->{'config'}->{'smtp'}->{'password'}; my $to = $xml->{'config'}->{'smtp'}->{'to'}; my $from = $xml->{'config'}->{'smtp'}->{'from'}; my $subject = $xml->{'config'}->{'smtp'}->{'subject'}; my $smtp = Net::SMTP_auth->new( $hostname ) or die "Could not +create SMTP connection"; $smtp->auth( 'LOGIN', $username, $password ); $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(); }

And here is my XML config file.

<?xml version="1.0"?> <importantdates> <config> <db> <username>your_db_username</username> <password>your_db_hostname</password> <hostname>yourdb.yourdomain.com</hostname> <database>databasename</database> </db> <smtp> <hostname>smtp.yourdomain.com</hostname> <username>your_smtp_username</username> <password>your_smtp_password</password> <to>whoisitto@somedomain.com</to> <from>thisisyou@yourdomain.com</from> <subject>Important Date Reminder</subject> </smtp> <reminders> <!--How often you want reminders. I want emai +l reminders 7 days before the event, the day before the event, and th +e day of the event--> <days>0</days> <days>1</days> <days>7</days> </reminders> </config> </importantdates>

My database looks like this:

d_id d_date d_desc
1 1978-03-05 Anne's Birthday
2 1975-05-06 Mom and Dad's Anniversary

etc.

    -Bryan


In reply to Important Date Email Reminder by mrborisguy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.