Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print matching items via sendmail

Status
Not open for further replies.

twantrd

Technical User
Feb 13, 2005
26
0
0
US
Hi there,

I'm trying to match a list of items and using sendmail to mail me only those matching strings from a file. This is the code I have so far:

Code:
use FileHandle;
my $match;

# Grab the filenames that have been downloaded
open (FILENAME, "<$logfile");
   while (my $match=<FILENAME>) {
         if ($match =~ /Starts:/) {
             print "$match"; # printing the files works
         }
   }
close (FILENAME);
   my $mail=new FileHandle;
   $mail->open("| /usr/sbin/sendmail -t") or die "Cannot open $!";
   $mail->print("From: root\@host.mydomain.com\n");
   $mail->print("To: $recipients\n");
   $mail->print("Subject: Downloaded files\n");
   $mail->print("*** Downloaded Files ***\n\nFiles: $match");
   $mail->close();

The body of the email message is completely blank. However, my "print $match;" shows the strings that I am matching for. What is wrong with my code? Thanks.

-twantrd
 
this

Code:
while (my $match=<FILENAME>) {
}

should be:

Code:
while ($match=<FILENAME>) {

but only the last match of $match will be sent since you are just looping through the whole file before sending the email.





- Kevin, perl coder unexceptional!
 
Thanks for spotting that out, Kevin. But, how do I save it so that it prints out ALL matching strings? I tried putting that in an array and it still emails me with nothing in the body. Thank you for your help.

-twantrd
 
Code:
use FileHandle;
my $match;
# Grab the filenames that have been downloaded
open (FILENAME, "<$logfile");
   while (<FILENAME>) {
         if (/Starts:/) {
             $match .= $_;
         }
   }
close (FILENAME);
   my $mail=new FileHandle;
   $mail->open("| /usr/sbin/sendmail -t") or die "Cannot open $!";
   $mail->print("From: root\@host.mydomain.com\n");
   $mail->print("To: $recipients\n");
   $mail->print("Subject: Downloaded files\n");
   $mail->print("*** Downloaded Files ***\n\nFiles: $match");
   $mail->close();

if that doesn't more or less do what you want post some samples lines from the logfile and explain what you are trying to find in the file and send in the email.

- Kevin, perl coder unexceptional!
 
Kevin,

Thank you so much! That was it. It works now. The missing piece was:

$match .= $_;

Again, I appreciate your help. Thank you!

-twantrd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top