Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send the content of file as email in Perl?

Tags:

file

email

perl

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.

like image 482
codingbear Avatar asked Aug 25 '09 00:08

codingbear


People also ask

How can you send email using Perl script?

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.

How do I send an email to multiple recipients in Perl?

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!\

How do I edit a file in Perl?

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.


1 Answers

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;
like image 60
karim79 Avatar answered Nov 15 '22 04:11

karim79