Mail the contents of a file in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Mail the contents of a file in perl
# 1  
Old 03-26-2010
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 ...

Code:
$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";


Last edited by radoulov; 03-26-2010 at 08:31 AM.. Reason: Please use code tags!
# 2  
Old 03-26-2010
Try this:

Code:
#!/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";

# 3  
Old 03-26-2010
You HAVE it in an array:

Code:
my @array_of_data = <DATA>;


So don't

Code:
print MAIL <DATA>;

do

Code:
print MAIL  @array_of_data ;

# 4  
Old 03-26-2010
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)
# 5  
Old 03-26-2010
Quote:
Originally Posted by cillmor
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?
# 6  
Old 03-26-2010
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:

Code:
#!/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

Last edited by radoulov; 03-26-2010 at 10:43 AM.. Reason: Added code tags.
# 7  
Old 03-26-2010
OK,
try to print the content of the variable file_content after populating it.
Put the following line of code:

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

above this line:

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




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

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

<body>
!



Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script for Calling a function and writing all its contents to a file

I have a function which does awk proceessing sub mergeDescription { system (q@awk -F'~' ' NR == FNR { A = $1 B = $2 C = $0 next } { n = split ( C, V, "~" ) if... (3 Replies)
Discussion started by: crypto87
3 Replies

2. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

New to Perl Mail @ sign search and replace in a file

HI I'm terribly new to perl .. I;ve been trying to use this command to search and replace entries in a file I tried this and it works perl -p -i -e 's/old/new/' filename Problem is that I have a list of email addresses and I need to serach and replace the entire email address as my... (5 Replies)
Discussion started by: mnassiri
5 Replies

4. Shell Programming and Scripting

Perl Help - Assigning variables to text file contents

I am looking to create a perl script which will take numbers from a simple text file, convert them from decimal to hex, and then rewrite those values in the file or create a new file with the hex numbers(whichever's easier). My text document for example would be something as simple as 1312... (6 Replies)
Discussion started by: samh785
6 Replies

5. Shell Programming and Scripting

Parse file contents in perl...

Hi, I have the file like this: #Contents of file 1 are: Dec 10 12:33:44 User1 Interface: Probe Dec 10 12:33:47 uSER1 SOME DATA Dec 10 12:33:47 user1 Interface: MSGETYPE Dec 10 12:34:48 user1 ID: 10. Dec 10 12:33:55 user1 Interface: MSGTYPE Dec 10 12:33:55 user1 Id: 9 ... (1 Reply)
Discussion started by: vanitham
1 Replies

6. Shell Programming and Scripting

Perl how to replace e-mail address from the file

I have a script which updates the users e-mail address according to wherever the users type in the browser. The script does other stuffs but this what i am struggling with ..lol. Basically, we are using the command below to try to update the e-mail, however since the e-mail address has "@" the perl... (2 Replies)
Discussion started by: cacm1975
2 Replies

7. Shell Programming and Scripting

how to read the contents of a file using PERL

Hi My requirement is to read the contents of a fixed length file and validate the same. But am not able to read the contents of the file and when i tried it to print i get <blank> as an output... I used the below satatements for printing the contents ... (3 Replies)
Discussion started by: meva
3 Replies

8. UNIX for Dummies Questions & Answers

Move contents of a directory into one file for e-mail distribution ...

Hello, Here is what I am trying to accomplish. I am going to have one directory in which there will be files of varying types (Excel, Word, PPT, and possible others), and I need to be able to be bundle however many files there are in there together in to one file to be used as an e-mail... (3 Replies)
Discussion started by: rip73
3 Replies

9. Shell Programming and Scripting

PERL - copy fiel contents to array then compare against other file

Basically to illuminate i want to take a file with mutliple lines, C:\searching4theseletters.txt a b c Read this into an array @ARRAY and then use this to compare against another file C:\inputletters.txt b o a c n a (9 Replies)
Discussion started by: bradleykins
9 Replies

10. Shell Programming and Scripting

Looking for a perl script (windows) to delete the contents of a file

Hi All, Im having a file named logserver.txt. I want a perl script to take a backup of that file, along with the datestamp; move the file to a different location or empty the contents of the file after backup. Remember, the file gets generated when the related service starts. My condition is... (14 Replies)
Discussion started by: ntgobinath
14 Replies
Login or Register to Ask a Question