tags:

views:

471

answers:

4

I have a log that gets created from a bunch of cron jobs. My task now is to send specific logs (e.g. error outputs) as an email. What is the best way to get content from a file and send it as an email?

I have already figured out how to send email in perl. I just need to figure out how to read in the file and put it as the text of the email.

+3  A: 

You can just slurp up the contents of the file like so and use it as you would any other string:

 open my $fh, '<', 'file.txt' or die "Ouch: $!\n";

 my $text = do {
   local $/;
   <$fh>
 };

 close $fh or die "Ugh: $!\n";
 print $text,"\n";
seth
Use `File::Slurp::read_file`.
Sinan Ünür
I think this is the way I was looking for. Can you explain the "my $text = do { ... };"? I'm really new to perl.
bLee
The block following 'do' is executed and the last line is returned (see `perldoc -f do`). The local $/ undefines the value of the input record separator so `<$fh>` gets the entire file. This is a fairly common perl idiom called file slurping. You could also use `File::Slurp::read_file` as Sinan recommended.
seth
@bLee if you are new to Perl, you should read the entire Perl documentation at least once. Type `perldoc perltoc` to get the list of contents. `perldoc perldoc` for information on `perldoc`.
Sinan Ünür
@Sinan: I'm so new that I didn't even know such thing (perldoc) exists. Thanks!
bLee
Just for clarification: there is a missing ; after <$fh> inside the do statement.
bLee
@bLee: perl doesn't need a semi-colon to end the last statement in the block.
seth
+1 to Sinan - perldoc is an exceptionally well-written and comprehensive piece of documentation.
Chris Lutz
+4  A: 

What are you using to send the email? I use MIME::Lite. and you can use that to just attach the file.

Otherwise you'd just open the log, read it in line at a time (or use File::Slurp) and dump the contents of the file into the email.

Devin Ceartas
+10  A: 

I use MIME::Lite, this is the cron script I use for my nightly backups:

$msg = MIME::Lite->new(
  From    => '[email protected]',
  To      => '[email protected]',
  Bcc     => '[email protected]',
  Subject => "DB.tgz Nightly MySQL backup!",
  Type    => "text/plain",
  Data    => "Your backup sir.");

$msg->attach(Type=> "application/x-tar",
             Path =>"/var/some/folder/DB_Dump/DB.tgz",
             Filename =>"DB.tgz");

$msg->send;
karim79
I like your way.
JDrago
See http://perldoc.perl.org/perlfaq9.html#How-do-I-send-mail%39
Sinan Ünür
Great example, but I didn't want the file attachment. +1
bLee
A: 

You can open a file in Perl in several ways.

What you need to know is described in perl -f open

Here is an example:

my $file = 'filename.txt';
open my $ifh, '<', $file
  or die "Cannot open '$file' for reading: $!";
local $/ = '';
my $contents = <$ifh>;
close( $ifh );

Now just email $contents in your email.

I'm not sure how you are sending email, but the way I use frequently is as follows:

# Install these modules from CPAN:
use Mail::Sendmail;
use MIME::Base64;

sendmail(
  To                          => '[email protected]',
  From                        => 'Friendly Name <[email protected]>',
  'reply-to'                  => '[email protected]',
  Subject                     => 'That file you wanted',

  # If you are sending an HTML file, use 'text/html' instead of 'text/plain':
  'content-type'              => 'text/plain',
  'content-transfer-encoding' => 'base64',
  Message                     => encode_base64( $contents ),
);
JDrago