Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Read a log file's content and send it in body of email

by kkurnala (Initiate)
on Mar 26, 2012 at 21:40 UTC ( #961761=perlquestion: print w/replies, xml ) Need Help??
kkurnala has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks I am a newbie into Perl. Trying to still learn nuts and bolts here. I am supposed to put together a perl script that would read a log file(text) and then e-mail it as body of e-mail using mime/sendmail. Can one of you guide me please !. Here is a piece of code i put together,but, not working though.
#!/usr/bin/perl -w use strict; use warnings; use Email::MIME::CreateHTML; use Email::Send; my $plain_text='text/plain'; my $sampletext; open(FH, "</apps/dryrun-merges/logs/dryrun-merge-report") || die("FAIL +ED to open dryrun merge report: $!"); #while(<FH>){ #$sampletext = $_; #} #close (FH); my $email = Email::MIME->create_html( header => [ From => '', To => ' #,,SJStep', Subject => 'Daily Dry-Run Merge Report - Test +Message', ], body => @lines, text_body => $plain_text ); my $sender = Email::Send->new({mailer => 'Sendmail'}); #$Email::sendmail = '/usr/sbin/sendmail'; #$sender->send($message) or die "Unable to send E-mail\n"; # $sender->mailer_args([Host => '']); $sender->send($email); print "About to send the E-mail, good-Bye!\n"; exit 0;
I am only getting a blank line like ---------- in the body of e-mail currently. The log file contains text like:
Summary of Conflicts:
Text Conflicts:
Tree Conflicts: Blah.. blah.. blah..

Replies are listed 'Best First'.
Re: Read a log file's content and send it in body of email
by tangent (Vicar) on Mar 26, 2012 at 22:58 UTC
    Where you say "body => @lines" there is nothing in @lines. To fix that insert at the top:
    my @lines = <FH>;
    However, unless your file is HTML your email will be garbled (e.g. it will not display linebreaks properly). If your file is not HTML then there is no need to use Email::MIME::CreateHTML, just use Email::Send:
    use strict; use warnings; use Email::Send; open(FH, "</apps/dryrun-merges/logs/dryrun-merge-report") or die("FAILED to open dryrun merge report: $!"); my @lines = <FH>; close(FH); my $message = <<'__MESSAGE__'; From: To:,, Subject: Daily Dry-Run Merge Report - Test Message __MESSAGE__ $message .= join('',@lines); my $sender = Email::Send->new({mailer => 'Sendmail'}); my $result = $sender->send($message); print "$result\n";
      Thank you very much Tangent, Really Appreciate your Time ! Respect for the Community !! Your suggesstion did really help me. Thanks, Krishna.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://961761]
Approved by kcott
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2018-05-26 03:09 GMT
Find Nodes?
    Voting Booth?