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.
Updateuse 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";
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 | |
|
Re^2: Logfile to HTML and MIME EMail table
by sidsinha (Acolyte) on Jul 22, 2013 at 05:42 UTC | |
by Loops (Curate) on Jul 22, 2013 at 06:28 UTC | |
by sidsinha (Acolyte) on Jul 22, 2013 at 07:37 UTC | |
by sidsinha (Acolyte) on Jul 24, 2013 at 00:37 UTC | |
by Loops (Curate) on Jul 24, 2013 at 01:08 UTC | |
by Anonymous Monk on Jul 25, 2013 at 19:33 UTC |