btobin0 has asked for the wisdom of the Perl Monks concerning the following question:

I created a very simple email script that gathers some UNIX command output, and then places it into a file. This file I want to then email back out with the input in the body.
#Execute Test Mail Script system ('mail briantest@server.com <test'); $SIG{INT} = sub { die "Just Send it!"}; $SIG{TERM} = sub { die "Do it now!"}; sleep(1); #Unix Commands my $PrStat = qx(prstat 1 1 | grep Total); my $Mail = qx(tail -15 /var/log/syslog | grep briantest); my $Iface = qx(ifconfig -a); #Text Capture File chomp($d=`date +%Y%m%d`); open F,">Report_$d.txt"; print F "Here is the Results from the Script\n\n"; print F "-------------------------------------\n\n"; print F $PrStat; print F "-------------------------------------\n\n"; print F $Mail; print F "-------------------------------------\n\n"; print F $Iface; print "This script has finished!\n\n"; print "Generating Email!\n\n"; #system ('mail test@server.com < Report_$d.txt);
I get the initial send, but once the file has been generated, I am try send it back out and the script completes without error, but no email. Please help.

Replies are listed 'Best First'.
Re: Email Question
by stevieb (Canon) on Sep 24, 2015 at 19:46 UTC

    Do you have use strict; and use warnings; enabled? Can you confirm that the report file is actually being written out?

    It isn't as simple as you having the system line that does the mailing commented out, can it? Note that it's missing its closing single-quote, but that doesn't matter... $d won't interpolate within single-quotes, so change them into double-quotes.

    Also try closing the file before sending the mail: close F or die $!;. You should also avoid using bareword file handles, and use the three-arg open: open my $wfh, '>', "Report_$d.txt" or die $!;, then replace the file handle F with $wfh. Here's an example using a "heredoc" to eliminate the numerous prints:

    my $report = <<EOF; Here is the Results from the Script ------------------------------------- $PrStat ------------------------------------- $Mail ------------------------------------- $Iface EOF open my $wfh, '>', "Report_$d.txt" or die $!; print $wfh $report; close $wfh or die $!; print "This script has finished!\n\n" . "Generating Email!\n\n"; system ("mail test@server.com < Report_$d.txt");
      Now that strict and warnings are in place the $d is looking for an explicit package. How do I fix this?

        Why does it complain about $d but not about $PrStat ?

        See strict and my.

Re: Email Question
by u65 (Chaplain) on Sep 24, 2015 at 23:08 UTC