#!/usr/bin/perl use strict; use warnings; use Class::Date qw(:errors date localdate gmdate now -DateParse -EnvC); # Paket: libclass-date-perl use Text::CSV; use Data::Dumper; $Class::Date::DATE_FORMAT="%Y-%m-%d"; my %config = ( "debug" => 3, # debug-level 0 or upto 3 "sendmail" => "/usr/sbin/sendmail -t", "from" => "root", # mail from header-field "db_file" => "./birthdaydb.csv", # database "reminder" => 5, # $reminder ); $config->{db}=load_db(\%config); for my $row (@{$config->{db}}) { print Dumper($row) if ($config{debug}>0); next unless( $row->[0] ); print "\tdate: $row->[0]\n". "\tname: $row->[1]\n". "\tmail: $row->[2]\n" if ($config{debug}>1); my $date = Class::Date->new("$row->[0]"); my $today = Class::Date->new(time); my $age = $today->year - $date->year; print "\tToday: $today; Birthday: $date\n" if ($config{debug}>1); $today -= [$age,0,-1]; print "\t? Today+1: $today == $date\n" if ($config{debug}>1); if ( $date->string ne $today->string ) { next if ( $config{reminder} <= 0 ); $today += [0,0,$config{reminder}-1]; print "\t? Today+$config{reminder}: $today == $date (reminder)\n" if ($config{debug}>1); next if ( $date->string ne $today->string ); } print "\tHAPPY ${age}th BIRTHDAY!\n" if ($config{debug}>0); $date += [$age,0,0]; sendmail(\%config, $row->[1], $row->[2], $date, $age); } ################################################################ sub load_db { my $config=shift; my @db; my $csv = Text::CSV->new(); open(my $fh, '<', $config->{db_file}) or die "error open $file ($!)\n"; while(my $row=$csv->$csv->getline($fh)) { push(@db,$row); } close($fh) or die "error close $file ($!)\n"; return \@db; } sub get_addresses { my $config=shift; my $exclude = shift; # exclude birthday child (by name) my $s; for my $row (@{$config->{db}}) { $s .= $row->[2]."," if (length($row->[2]) && $row->[1] ne $exclude); } print "\texclude: $exclude\n\t$s\n" if ($config->{debug}>1); return $s; } sub sendmail { my $config=shift; my $user=shift; my $reply=shift; my $date=shift; my $age=shift; my $to=get_addresses($config, $user); my $mail=<{from} Reply-To: $reply To: $to Subject: Der Geburtstag von $user User-Agent: HAPPY BIRTHDAY! !!! Test Betrieb der beta-Version vom HAPPY BIRTHDAY! Mailer !!! Hallo, diese Mail soll daran erinnern, das $user am $date $age Jahre alt wird. Mit freundlichen Grüßen \tIhr HAPPY BIRTHDAY! Team. PS: Diese E-Mail wurde automatisch verschickt. . EOM print $mail if ($config->{debug}>2); # open(SENDMAIL, "|-", $config->{sendmail}) || die ("$config->{sendmail}: $!\n"); # print SENDMAIL $mail; # close(SENDMAIL); }