Mail the contents of a file in perl

Hi,

I'm trying to read the contents of a file (message.txt), put them in a mail and then mail it
This is what I have thus far but I having trouble referencing the file. I'm trying to put it into an array so any ideas would be helpful ...

$to='user.n@domain.com';
$from= 'username';
$subject='Test';

my $log_file = "/message.txt";

open FILE, "$log_file" or die $!;
my @array_of_data = <DATA>;

open(MAIL, "|/usr/sbin/sendmail -t");

## Mail Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
## Mail Body
print MAIL <DATA>;

close(MAIL);
close FILE;
close (DATA);

print "A message has been sent from $from to $to\n";

Try this:

#!/usr/bin/env perl

use warnings;
use strict;


my $to = 'user.n@domain.com';
my $from = 'username';
my $subject = 'Test';

my $log_file = '/message.txt'; # are you sure it's /message 
                               # and not ./message?

my $file_content;

{                         
  local $/ = undef;
  open FILE, '<', $log_file or die "open $log_file: $!\n";
  $file_content = <FILE>;
  close FILE or warn "close $log_file: $!\n";
  }
  
open MAIL, "|/usr/sbin/sendmail -t";

## Mail Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
## Mail Body
print MAIL $file_content;

close MAIL;

print "A message has been sent from $from to $to\n";

You HAVE it in an array:

my @array_of_data = <DATA>;

So don't

print MAIL <DATA>;

do

print MAIL  @array_of_data ;

Tried both but body of the mail is blank.

my $log_file = '/message.txt'; # are you sure it's /message
# and not ./message?

/message.txt is the location of the file that I want to read and pass into the mail (root level)

And there are no errors?

Are you sure the log file isn't empty?

Script runs without error, mail is sent but the body is blank.
The log file is not empty, it just contains "This is email text".

If I run it under shell, for example:

#!/bin/sh
mail -s "PM_Logs" "cunningham.p@euro.apple.com" < /message.txt
exit 0

it runs fine (mail sent with content from message.txt) but under perl no luck.

thanks again for the help

OK,
try to print the content of the variable file_content after populating it.
Put the following line of code:

print "File content is: \n$file_content\n\n";

above this line:

open MAIL, "|/usr/sbin/sendmail -t";

If the output is correct, try to run the command using sendmail (not mail) on the command line:

cat <<! | /usr/sbin/sendmail -t
To: <to>
From: <from>
Subject: <subject>

<body>
!

I got it working as expected. Thanks alot radoulov