Thanks to both Uri and John for their input.  I've added your 
suggestions and the script is much cleaner and more concise now.

        I decided to keep the header info in the sub because I'm now setting 
the subject to the path of the error log, so at a glance I know where there's a 
problem.

On Jul 18, 2011, at 8:50 PM, Uri Guttman wrote:

> it would be much cleaner IMO to collect the paths of the files you want
> and then process those paths in a sub. the rule is generally to collect
> the data you need and process later and elsewhere.

        I'm sorry, but I'm not following you here.  Could you explain a little 
more?

> but beyond this calling sendmail directly isn't cool anymore. there are
> many useful mail modules that make this easier and work with other
> mailers or directly with smtp. 

        I searched for mail modules and there seems to be a million of them.  
Could you recommend a couple that you like?

Thanks again,
Marc

------------------

#!/usr/bin/perl

use strict;
use warnings;

use File::Find;
use File::Slurp;

my $path_to_search = $ENV{HOME}.'/public_html';
my $file_name      = 'error_log';
my $from_address   = 'f...@me.com';
my $to_address     = 't...@you.com';
my $mail_app       = '/usr/sbin/sendmail';


find(\&wanted, $path_to_search);

sub wanted {
        return if $_ ne $file_name;

        my $subject = substr $File::Find::name, length($path_to_search);  # 
removes "/home/USER/public_html" from the subject
        my $log_text = read_file($File::Find::name);
        my $message = <<MSG ;
MIME-Version: 1.0
Content-Type: text/plain
To: $to_address
From: $from_address
Subject: $subject

$log_text
MSG

        open (my $mail_fh, '|-', $mail_app, '-t', '-oi', '-oem') or die "Can't 
open '$mail_app' because: $!";
                print {$mail_fh} $message;
        close ($mail_fh) or warn $! ? "Error closing '$mail_app' pipe: $!" : 
"Exit status $? from '$mail_app'";

        unlink( $File::Find::name ) or die "Cannot unlink '$File::Find::name' 
because: $!";

}
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to