Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Logfile to HTML and MIME EMail table

by sidsinha (Acolyte)
on Jul 22, 2013 at 00:28 UTC ( #1045554=perlquestion: print w/ replies, xml ) Need Help??
sidsinha has asked for the wisdom of the Perl Monks concerning the following question:

Hellow Perl Monks, I am kinda new perl user, so sorry for basic question. I tried some stuff learning frm the internet but it dint work out for me hence came to the place I love, perlmonks!!! Please save my day. I have a script which I wrote that reads last 24 hr information from a log file, and emails it to me in a good format. However, I would like this table to be printed in HTML table format and send it as a mail to me in HTML format. Below is the script that works for me now:
use HTML::Table; use Mail::Sendmail; my $outfile = 'log.txt'; #Read the last 24 lines from the file which is the latest 24 transfers + and generate a email. unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , 'mail.dadada.com'; $Mail::Sendmail::mailcfg{'from'} = "dadada\@dadada.com"; open(FILE, $outfile) or die "Can't open File: File does not exist $!"; @file = <FILE>; @file = reverse(@file); close FILE; for (0..23) { $dayData .= "$file[$_]"; } my %mail = ( To => 'dadada@dadada.com', Subject => "Report - Transfer Summary - $date", Message => "Mail auto generated on $date\n\n\n $dayData", ); sendmail( %mail ) or die "Error: $Mail::Sendmail::error\n"; printf "\nMail Sent /@ $date\n";
The content of the email outputs as below:
Sun Jul 21 07:20:22 2013 15 Success 17 sec + 0.88 MB/sec 7.00 Mb/sec 52.00 MB/min Sun Jul 21 06:20:22 2013 16 Success 17 sec + 0.94 MB/sec 7.00 Mb/sec 56.00 MB/min Sun Jul 21 05:20:22 2013 16 Success 17 sec + 0.94 MB/sec 7.00 Mb/sec 56.00 MB/min Sun Jul 21 04:20:22 2013 16 Success 17 sec + 0.94 MB/sec 7.00 Mb/sec 56.00 MB/min Sun Jul 21 03:20:22 2013 16 Success 17 sec + 0.94 MB/sec 7.00 Mb/sec 56.00 MB/min Sun Jul 21 02:20:22 2013 16 Success 18 sec + 0.89 MB/sec 7.00 Mb/sec 53.00 MB/min Sun Jul 21 01:20:22 2013 16 Success 19 sec + 0.84 MB/sec 6.00 Mb/sec 50.00 MB/min Sun Jul 21 00:20:22 2013 16 Success 18 sec + 0.89 MB/sec 7.00 Mb/sec 53.00 MB/min Sat Jul 20 23:20:23 2013 16 Success 16 sec + 1.00 MB/sec 8.00 Mb/sec 60.00 MB/min Sat Jul 20 22:20:23 2013 16 Success 18 sec + 0.89 MB/sec 7.00 Mb/sec 53.00 MB/min Sat Jul 20 21:20:23 2013 16 Success 17 sec + 0.94 MB/sec 7.00 Mb/sec 56.00 MB/min Sat Jul 20 20:20:22 2013 16 Success 17 sec + 0.94 MB/sec 7.00 Mb/sec 56.00 MB/min Sat Jul 20 19:20:23 2013 16 Success 17 sec + 0.94 MB/sec 7.00 Mb/sec 56.00 MB/min Sat Jul 20 18:20:23 2013 16 Success 17 sec + 0.94 MB/sec 7.00 Mb/sec 56.00 MB/min Sat Jul 20 17:20:22 2013 16 Success 18 sec + 0.89 MB/sec 7.00 Mb/sec 53.00 MB/min Sat Jul 20 16:20:22 2013 16 Success 18 sec + 0.89 MB/sec 7.00 Mb/sec 53.00 MB/min Sat Jul 20 15:20:23 2013 16 Success 17 sec + 0.94 MB/sec 7.00 Mb/sec 56.00 MB/min Sat Jul 20 14:20:23 2013 16 Success 17 sec + 0.94 MB/sec 7.00 Mb/sec 56.00 MB/min Sat Jul 20 13:20:23 2013 16 Success 18 sec + 0.89 MB/sec 7.00 Mb/sec 53.00 MB/min Sat Jul 20 12:20:22 2013 136 Success 2 min 39 s +ec 0.86 MB/sec 6.00 Mb/sec 51.00 MB/min Sat Jul 20 11:20:22 2013 16 Success 18 sec + 0.89 MB/sec 7.00 Mb/sec 53.00 MB/min Sat Jul 20 10:20:23 2013 16 Success 17 sec + 0.94 MB/sec 7.00 Mb/sec 56.00 MB/min Sat Jul 20 09:20:25 2013 16 Success 18 sec + 0.89 MB/sec 7.00 Mb/sec 53.00 MB/min Sat Jul 20 08:20:23 2013 16 Success 17 sec + 0.94 MB/sec 7.00 Mb/sec 56.00 MB/min

but you know its just plaintext. I tried using the module HTML::Table, but pls help me illustrating it and using it to send colorful table in email. Thanks!!! Sid

Comment on Logfile to HTML and MIME EMail table
Select or Download Code
Re: Logfile to HTML and MIME EMail table
by Loops (Hermit) on Jul 22, 2013 at 02:35 UTC

    There are several facets to your question. One is how to create an HTML table, and the other is how to generate and send an HTML formatted email. Below is some code that roughly accomplishes each of those tasks. Much could be done to improve the code further, but it should still enable you to get started.

    use strict; use warnings; use MIME::QuotedPrint; use HTML::Table; use Mail::Sendmail; # Read lines open(FILE, 'log.txt') or die "Can't open File: File does not exist $!" +; my @file = <FILE>; chomp @file; @file = reverse(@file); close FILE; # Create Table my $table = new HTML::Table; $table->addRow(split /\s\s+/) for (@file[0..23]); $table->setColBGColor(3, 'Green'); $table->setColFormat(1, '<font color="red">', '</font>'); $table->setColFormat(5, '<font color="blue">', '</font>'); # Create HTML email with table content unshift @{$Mail::Sendmail::mailcfg{'smtp'}}, 'localhost'; my $plain = encode_qp "html log included"; my $html = encode_qp $table; my $boundary = "====" . time() . "===="; my $date = Mail::Sendmail::time_to_date(); my %mail = ( From => 'me@localhost', To => 'user@host.com', Subject => "Report - Transfer Summary - $date", 'content-type' => "multipart/alternative; boundary=\"$boundary\"", ); $mail{body} = <<END_OF_BODY; --$boundary Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable $plain --$boundary Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable <html>$html</html> --$boundary-- END_OF_BODY # Send email and finish sendmail( %mail ) or die "Error: $Mail::Sendmail::error\n"; printf "\nMail Sent @ $date\n";
    Update

    Here is more succinct code that uses a couple different CPAN modules to accomplish the goal:

    use strict; use warnings; use HTML::Table; use MIME::Lite; use File::ReadBackwards; my $table = new HTML::Table; my $log = File::ReadBackwards->new('log.txt') or die $!; $table->addRow(split(/\s\s+/, $log->readline)) for (1..24); $table->setColBGColor(3, 'Green'); $table->setColFormat(1, '<font color="red">', '</font>'); $table->setColFormat(5, '<font color="blue">', '</font>'); $log->close; my $msg = MIME::Lite->new( From =>'me@localhost', To =>'recipient@somewhere', Subject =>'Transfer Summary', Type =>'text/html', Encoding =>'base64', Data =>$table, ); $msg->send;
      @ Loops!!! Excellent, you are my hero. This is exactly what I wanted. I am customizing it to my needs and adding a few other things, but this really made me understand how to use the module. Thankyou very much. If I have questions I shall post it :)
      Two More questions: Is it possible to have alternating row colors (i read a documentation where we can define evenrowclass and oddrowclass while defining a table using HTML::Table. But it doesnt say how to define a class. -evenrowclass=>'even', -oddrowclass=>'odd', 2. Is it possible to change the font face of the table? or we have to specifically give col/row wise font as below:
      $table->setColFormat(1, '<font color="red" face="Calibri">', '</font>' +);
      Is it possible to have borders for the table? I mean say black border color. I could only find spacing option and border (0/1). Thanks!!!

        Remember that the table is being created in HTML so any formatting you can do in HTML you can do in your Perl code. Some email clients don't do a great job with HTML styling, but this is for your own consumption so there's little to worry about there. For instance, if your email client supports CSS (mine doesn't) you can include CSS styles that will change the font for the entire table. If not, you're stuck with setting the individual cell styles or formats.

        There are many resources online for HTML, but here are a couple hints for your questions.

        # Set background color for alternate rows $table->setRowBGColor($_ * 2, '#9999CC') for (1..12); # Set a border and make it green $table->setStyle('border:4px solid green;');

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1045554]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2014-09-18 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (111 votes), past polls