#!/usr/bin/perl -w my $FROM = 'absender@irgendwo.com'; my @TO = qw(logs@perlmeister.com); my $SUBJECT = 'Today\'s Logs'; my $MAXLINES = 10; my $DATABASE = '/home/mschilli/tmp/logmail.dat'; my $B = '/home/mschilli/tmp'; my @FILES = ("Neukunden" => "$B/rbsub.txt", "Fehler" => "$B/errors.txt", ); use strict; use Mail::Mailer; use Storable; my $offs = {}; if(-r $DATABASE) { # Gespeicherte Daten auslesen $offs = retrieve($DATABASE) or die "Cannot open $DATABASE"; } my $mailtext = ""; while(my ($text, $file) = splice(@FILES, 0, 2)) { # Teilüberschrift $mailtext .= "\n" . "#" x 70; $mailtext .= "\n$text:\n"; $mailtext .= "#" x 70 . "\n"; # Gespeicherter Offset vom letzten Mal my $offset = ($offs->{$file} || 0); if(open(FILE, "<$file")) { if(-s $file < $offset) { # Datei wurde gekürzt $offs->{$file} = -s $file; $mailtext .= "(Offset adjusted)\n"; } else { # Bis zum gespeicherten Offset # vorfahren seek(FILE, $offset, 0); # Rest zeilenweise auslesen my @data = ; my $noflines = @data; # Neuen Offset einstellen $offs->{$file} = tell(FILE); # Kürzen falls > $MAXLINES if(@data > $MAXLINES) { splice(@data, $MAXLINES/2, @data-$MAXLINES, "...\n"); } my $data = join '', @data; # Zeilenzahl ausgeben $mailtext .= sprintf "(%d line%s)\n", $noflines, $noflines == 1 ? "" : "s"; # Logdaten ausgeben $mailtext .= $data; close(FILE); } } else { $mailtext .= "Cannot open $file\n"; } } mailto($SUBJECT, $mailtext, $FROM, @TO); store($offs, $DATABASE) or die "Cannot store data in $DATABASE"; ################################################## sub mailto { ################################################## my ($subj, $text, $from, @to) = @_; return if @to == 0; my $mailer = Mail::Mailer->new( 'sendmail' ); my $to = join ", ", @to; $mailer->open( { "To" => $to, "From" => $from, "Subject" => $subj, "Reply-To" => $from, } ) or die "open failed"; print $mailer $text; $mailer->close(); }