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

I have a program that is run on numerous AIX servers, that will collect information and then compare it to the previous day’s info. It will then email a list of any differences.

I am running into an issue where a new line will not always print after certain statements. It appears to be an issue with certain variables that are presented in these print statements. The strange thing is if I take the program through debug and view the array it shows that each statement is on a new line. The log prints correctly, but the email does not. Code:

$message = "A difference in /opt/local/bin has been noted: $opt_local_ +bin{$file}{'new'}{'file'} is now available\n"; push @messages, $message;
then I will present them in a single variable and email them:
$message = join('',@messages); $subject = "CM Audit Daily Changes Notification"; &mail_admin($subject,$message);
The result I get from this is, where *NEWLINE* is the next difference the program finds:
A difference in /opt/local/bin has been noted: clean_oratab is no longer available *NEWLINE*A change in /etc/hosts has been detected. 10.40.250.3 r2dmn1 # regatta 2 domain 1 switch is no longer available

If I join with a new line ($message = join('\n',@messages);) I get:
A difference in /opt/local/bin has been noted: clean_oratab is no longer available

A change in /etc/hosts has been detected. 10.40.250.3 r2dmn1 # regatta 2 domain 1 switch is no longer available

But I do not want the whitespace between the lines.

If I use ‘’ between the variable and end of the line it works, but it prints the ‘’:

$message = "A difference in /opt/local/bin has been noted: ‘$opt_local +_bin{$file}{'new'}{'file'} is now available’\n"; push @messages, $message;
gives the following result:

A difference in /opt/local/bin has been noted: ‘clean_oratab is no longer available’
A change in /etc/hosts has been detected. 10.40.250.3 r2dmn1 # regatta 2 domain 1 switch is no longer available

Obviously it is something in the variable output on certain lines. But again it only affects the emailed results, not anything I can see in debug or the log files which is an exact copy of what is sent to the email program.

Any ideas before I give up?

Replies are listed 'Best First'.
Re: New Lines are not printing after certain messages
by mhearse (Chaplain) on Mar 31, 2008 at 21:13 UTC
    I used to send emails that way. You might have better luck using MIME::Lite. If you can't just pipe the mail to /bin/mail, try sending it as text attachment. Text attachments load in the message body. It has worked well for me:
    my $message = join("\n" ,@messages); use MIME::Lite; my $msg = MIME::Lite->new( From => 'admin@yourdomain.net', To => 'users@yourdomain.net', Subject => 'CM Audit Daily Changes Notification', ); $msg->attach( Type => 'TEXT', Data => $message ); $msg->send();
Re: New Lines are not printing after certain messages
by pc88mxer (Vicar) on Mar 31, 2008 at 19:30 UTC
    What email client are you using, and what module or mechanism does &mail_admin use to send mail?
      we are using Outlook 2003.
      sub mail_admin { local($sub,$msg) = @_; $curAdmins = "/opt/local/etc/cur_admins"; @cur_admin_lines = `cat $curAdmins`; chomp(@cur_admin_lines); foreach $cur_admin_line (@cur_admin_lines) { ($id,$name,$group,$OncallFlag,$address) = split(/:/, $cur_admin_li +ne); $cur_admin_addresses .= " $address"; } my $cmd = "echo \"$msg\" | mail -s \"$sub\" $cur_admin_addresses"; system("$cmd"); my($rc) = $? / 256; if ($rc != 0) { &pager("Can't send mail_admin message $msg - rc = $rc", P); } }
        • "$cmd"
          needless creates a copy of $cmd. Just use
          $cmd

        • local?!
          local($sub,$msg) = @_;
          should be
          my($sub,$msg) = @_;

        • What's with launching another process to read a file?
          @cur_admin_lines = `cat $curAdmins`;
          should be

          my @cur_admin_lines = do { open(my $fh, '<', $curAdmins) or die("Unable to read list of admins ($curAdmins): $!\n"); <$fh> };
        • echo? print!

          my $cmd = "echo \"$msg\" | mail ..."; system("$cmd");

          should be

          my $cmd = "mail ..."; open(my $fh, '|-', $cmd) or ...; print $fh $msg; close($fh);
        • And it's already been pointed out that $msg and $sub aren't properly converted from string to shell argument literals. Using MIME::Lite would avoid that problem.

        How is that working at all?
        Since you're building a scalar ($cmd) that is getting interpolated before getting called by system, the message and subject are not literals (they're interpolated as well)
        What happens when $msg is this:
        $msg = '"|touch file_i_shouldnt_be_allowed_to_create|"';

        I imagine it's at least part of the source of your troubles (and highly dangerous from a security standpoint).

Re: New Lines are not printing after certain messages
by holli (Abbot) on Apr 01, 2008 at 03:15 UTC
    Code quality aside, could it be you're simply sending a html-mail? That would explain the vanishing of newlines. Try adding <br> tags instead of the linebreaks and see if that changes something.


    holli, /regexed monk/