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.
I use MIME::Lite, this is the cron script I use for my nightly backups:
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:
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:
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 just slurp up the contents of the file like so and use it as you would any other string:
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:
We can use
mail::outlook
instead ofmime::lite
too: