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.
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";
I use MIME::Lite, this is the cron script I use for my nightly backups:
$msg = MIME::Lite->new(
From => 'backup-bot@mydomain.com',
To => 'test@example.com',
Bcc => 'test@example.com',
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;
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.
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 => 'you@your-domain.com',
From => 'Friendly Name <friendly@server.com>',
'reply-to' => 'no-reply@server.com',
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 ),
);
I think attachments are the way to go given what you described and others have already contributed about this but if you have a requirement or need to read a file and parse it into a content of email (without attachments) via Perl here is the way to do it:
#!/usr/bin/perl
# this program will read a file and parse it into an email
use Net::SMTP;
#you need to change the four below line
my $smtp = Net::SMTP->new("your_mail_server_goes_here");
my $from_email = "your_from_email";
my $to_email = "yuor_to_email";
my $file="the_full_path_to_your_file_including_file_name";
my $header = "your_subject_here";
$smtp->mail($from_email);
#Send the server the 'Mail To' address.
$smtp->to($to_email);
#Start the message.
$smtp->data();
$smtp->datasend("From: $from_email\n");
$smtp->datasend("To: $to_email\n");
$smtp->datasend("Subject: $header \n");
$smtp->datasend("\n");
#make sure file exists
if (-e $file) {
$smtp->datasend("testing \n\n");
#read the file one line at a time
open( RFILE, "<$file" )||print "could not open file";
while (my $line = <RFILE>){
$smtp->datasend("$line");
}
close(RFILE) || print "could not close file";
}
else {
print "did not find the report $file ";
exit 1;
#End the message.
$smtp->dataend();
#Close the connection to your server.
$smtp->quit();
#Send the MAIL command to the server.
$smtp->mail("$from_email");
We can use mail::outlook
instead of mime::lite
too:
#open file from local machine
open my $fh, '<', "C:\\SDB_Automation\\sdb_dump.txt" or die "Ouch: $!\n";
my $text1 = do {
local $/;
<$fh>
};
close $fh or die "Ugh: $!\n";
print $text1,"\n";
#create the object
use Mail::Outlook;
my $outlook = new Mail::Outlook();
# start with a folder
my $outlook = new Mail::Outlook('Inbox');
# use the Win32::OLE::Const definitions
use Mail::Outlook;
use Win32::OLE::Const 'Microsoft Outlook';
my $outlook = new Mail::Outlook(olInbox);
# get/set the current folder
my $folder = $outlook->folder();
my $folder = $outlook->folder('Inbox');
# get the first/last/next/previous message
my $message = $folder->first();
$message = $folder->next();
$message = $folder->last();
$message = $folder->previous();
# read the attributes of the current message
my $text = $message->From();
$text = $message->To();
$text = $message->Cc();
$text = $message->Bcc();
$text = $message->Subject();
$text = $message->Body();
my @list = $message->Attach();
# use Outlook to display the current message
$message->display;
# create a message for sending
my $message = $outlook->create();
$message->To('xyz@.com');
$message->Subject('boom boom boom');
$message->Body("$text1");
$message->Attach('C:\SDB_Automation\sdb_dump.txt');
$message->send;