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

I am attempting to send a file that has been created by a
Perl script to an email address. I am merely adding this
feature to the script, and this is the first time I have
worked with PerlBelow is some of the code:

$rptname= "summary".$date.".txt"; print "Date: ",$date,"\n"; print "Report: ", $rptname,"\n"; open (RPT, "> $rptname") || warn "Unable to open report file: $!\n"; system (mailx -s \"Summary Report\" myname\@mycompany.com < $rptname") +;


I would greatly appreciate any help that can be given. Thank you

Replies are listed 'Best First'.
Re: How to send email
by btrott (Parson) on Jul 06, 2000 at 01:09 UTC
    What file are you trying to send? The file at $rptname? Because with your open line, you just wiped out the contents of $rptname. Whatever was in there before is now gone, so there's nothing to feed into mailx.

    Do you need to create a file? You could do all of this in memory. Just create a scalar that contains your mail message. Then send it using one of the Mail:: modules. Mail::Mailer may do what you want:

    use Mail::Mailer; my $mailer = new Mail::Mailer 'sendmail'; $mailer->open( { To => 'foo@bar.com', Subject => 'Report' } ); ## Create report in scalar $report... then write it. print $mailer $report; $mailer->close;
    You can also use SMTP or mail to send your mail.
      Just as a quick addition to btrott's comment on using smtp, as an example you could glance at this quick little post I made a few days back here I think it could fit right into the program easily..
Re: How to send email
by ZZamboni (Curate) on Jul 06, 2000 at 04:12 UTC
Re: How to send email
by httptech (Chaplain) on Jul 06, 2000 at 01:07 UTC
    It looks like you are missing the leading quotation mark in your system call. Running perl -wc on your code will help you find small errors like this.

    Update: I ran perl -wc on this code and it didn't spit out any errors, so it wouldn't have helped you in this case. It's still a good idea to use it though :)

    I would try and shy away from writing the mail to a temp file though; it will be a headache for you if the directory or file permissions ever get changed. I would probably do something like:

    open (MAIL, "|/usr/sbin/sendmail -t -oi -em") || die "Couldn't open sendmail: $!"; print MAIL <<EOM; To: me\@mycompany.com From: myscript\@mycompany.com Subject: Some subject $reportdata EOM close (MAIL);