Thread happy birthday mailer
(18 answers)
Opened by mika at 2011-02-02 22:49 Code (perl): (dl
)
1 2 3 4 5 my @db; open(my $fh, "<", $config{db}) or die "$config{db}: $!"; while (<$fh>){ push(@db, $_); } besser: Code (perl): (dl
)
1 2 3 4 my @db; open(my $fh, "<", $config{db}) or die "$config{db}: $!"; my @db=<$fh> chomp(@db); # <= \n entfernen dann gibt es keine Probleme damit Weiterhin kannst das split der Zeilen auch direkt beim Lesen machen machen. Code (perl): (dl
)
1 2 3 4 5 my @db; open(my $fh, "<", $config{db}) or die "$config{db}: $!"; my @db=<$fh> chomp(@db); # <= \n entfernen dann gibt es keine Probleme damit $_=[split(/;/,$_)] for(@db); Aber das Spitten solltest du von einem Modul machen lassen z.B. ![]() Mal als Beispiel (ohne es getestet zu haben): Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 #!/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=<<EOM; From: $config->{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); } Edit: "[...]" im zweiten Codeblock ergänzt Last edited: 2011-02-04 04:50:21 +0100 (CET) |