#!/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 "Could 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(); } #### your_db_username your_db_hostname yourdb.yourdomain.com databasename smtp.yourdomain.com your_smtp_username your_smtp_password whoisitto@somedomain.com thisisyou@yourdomain.com Important Date Reminder 0 1 7