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.
Just like the SendMail Utility, MIME::Lite Module also allows the users to send HTML formatted mails using the perl-script. $message = '<h1>H1 is the main heading</h1><p><img src="image_source_with_extension" /></p>'; The above code will format your mail by following HTML syntax.
Do it like this: use Net::SMTP::Multipart; $to1 = "sam\@bogus.com"; $to2 = '[email protected]'; $smtp = Net::SMTP::Multipart->new($smtpserver); $smtp->Header(To => [ $to1, $to2, '[email protected]' ], From => "junk\@junk.com", Subj => "This is a test."); $smtp->Text("Hello, world!\
Solution. Open the file in update mode ( "+<" ), read the whole file into an array of lines, change the array, then rewrite the file and truncate it to its current seek pointer.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With