Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Logfile to HTML and MIME EMail table

by Loops (Curate)
on Jul 22, 2013 at 02:35 UTC ( #1045564=note: print w/replies, xml ) Need Help??

in reply to Logfile to HTML and MIME EMail table

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 => '', 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";

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;

Replies are listed 'Best First'.
Re^2: Logfile to HTML and MIME EMail table
by sidsinha (Acolyte) on Jul 22, 2013 at 05:12 UTC
    @ 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 :)
Re^2: Logfile to HTML and MIME EMail table
by sidsinha (Acolyte) on Jul 22, 2013 at 05:42 UTC
    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;');
        Hey Loops! Thankyou , this is a great deal of info for did the job. May sound very basic, but am from a networking domain so very new to perl. Great learning from you! Adios :)
        Hi loops, When I use this line below:
        Line 25: $Wtable->addRow(split(/\s\s+/, $Wlog->readline)) for (1..24); +
        it gives me the error while compiling:
        Use of uninitialized value in split at line 25. Use of uninitialized value in split at line 25. Use of uninitialized value in split at line 25.
        This is dependent on the input file? The input file contains:
        Tue Jul 23 14:23:20 2013 17628 KB 4030 KB 77% Tue Jul 23 14:23:20 2013 17628 KB 4030 KB 77% Tue Jul 23 14:23:20 2013 17628 KB 4030 KB 77%
        the split looks ok and the output is fine but somehow this errors come up.
        Thanyou very much loops.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1045564]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2017-02-21 14:34 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (312 votes). Check out past polls.