Gossamer Forum
Home : General : Perl Programming :

read a text file and send by mail with perl script

Quote Reply
read a text file and send by mail with perl script
hi

I try to read a simply text file and send contents by mail

I have a perl script, but it doesn't work:

open(FILE, "/var/log/log.txt");
$content=<FILE>;
close(FILE);

open(MAIL, "|/usr/sbin/sendmail -t");
print MAIL "to:adress\@mydomain.xx\n";
print MAIL "from:server\@mydomain.xxt\n";
print MAIL "subject:log file !\n";
print MAIL "$content\n\n";
close(MAIL);


I get error mesage for 2 lines:

$content=<FILE>;

" Global symbol "$content" requires explicit package name "

and

print MAIL "$content\n\n";

" Global symbol "$content" requires explicit package name "

kind regards
ccc

Last edited by:

ccc: Feb 26, 2004, 4:20 PM
Quote Reply
Re: [ccc] read a text file and send by mail with perl script In reply to
Sounds like you have 'strict' turned on. Try something like;

Code:
#!/usr/bin/perl

use strict;

open(FILE, "/var/log/log.txt") || die "Cant open file. Reason: $!";
my $content=<FILE>;
close(FILE);

open(MAIL, "|/usr/sbin/sendmail -t") || die "Cant send mail. Reason: $!";
print MAIL 'to:adress@mydomain.xx \n';
print MAIL 'from:server@mydomain.xx \n';
print MAIL 'subject:log file !\n';
print MAIL '$content\n\n';
close(MAIL);

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] read a text file and send by mail with perl script In reply to
thanks.
but it doesn't work .
I get the mail without file contents and don't get any errors.

kind regards
ccc
Quote Reply
Re: [ccc] read a text file and send by mail with perl script In reply to
Change:

print MAIL '$content\n\n';

to:

print MAIL "$content\n\n";

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [dan] read a text file and send by mail with perl script In reply to
hi dan

I've changed.
I get the mail, but NO CONTENTS !

best regards
ccc

Quote Reply
Re: [ccc] read a text file and send by mail with perl script In reply to
Hello ccc ,

Remark out the file opening stuff and just
define : my $contents = "Hello world"; and send a test mail to see if you get Hello world .


I suspect you are getting nothing into $content from the file.

Try with caution to the log.txt's file size :

my $content; # define for strict

open(FILE, "/var/log/log.txt") || die "Cant open file. Reason: $!"; # (-:
while(<FILE>) {
$content .=$_; # get all of the file into content including past new lines
}
close(FILE);


second caution :
The above code will suck the entire file into $contents and memory and try to mail it !
Do not try to mail a file contents that is BIG. (-:


Then continue with your mailing part.

Thanks cornball

Last edited by:

cornball: Feb 29, 2004, 8:55 PM
Quote Reply
Re: [cornball] read a text file and send by mail with perl script In reply to
hallo cornball

thanks.
now it works !

kind regards
ccc
Quote Reply
Re: [ccc] read a text file and send by mail with perl script In reply to
 
Change: $content=<FILE>;
To:
@content=<FILE>;

The scalar context ($prefix) will only return one record at a time. The list context (@ prefix) will return a list of all records.