http://qs1969.pair.com?node_id=1001443

bh_perl has asked for the wisdom of the Perl Monks concerning the following question:


hi

This is sample HTML file which is called as sample.html
<html> <body> <table width="531" border="0" align="center" cellpadding="0" cellspacing="0"> <tbody> <tr> <td width="512" valign="top"> <table width="805" cellspacing="0" cellpadding="0" border="0"> <tbody> <tr> </tr> <tr> <td width="4" valign="top" rowspan="3">&nbsp;</td> <td width="948"> <div align="right"> <blockquote> <h2 align="right"><b>Online e-Statement </b></h2> </blockquote> </div></td> <td width="1" valign="top" rowspan="3"></td></tr> <tr> <td> <table width="800" cellspacing="0" cellpadding="20" border="0"> <tbody> <tr> <td width="564" valign="top"> <p align="left"><b>Dear BH_PERL,<br> <br> </b>Thank you for subscribing to Online E-Statement. As our valued cus +tomer, your satisfaction is our utmost priority. To view or open your statement, please key-in the predefined password as xxxx. <br> <br> <br> Thank you </br> <br></p><p align="left">Sincerely, <br> </p> </td> <td width="304" valign="top"> <p align="left"><span><strong>Bill Date</strong></span><br>10 Septembe +r 2012</p> <p align="left"><strong>PAYMENT DUE DATE</strong><br>30 September 2012</p> </td> </tr> </tbody> </table></td></tr> <tr> </tr> <tr> <td valign="top" colspan="3"></td></tr></tbody></table></td> </tr> </tbody> </table> </body> </html>

This is sample of my readHTML.pl script. It is not completed yet due to this problem. But i attached here for your reference.
#!/usr/bin/perl use Getopt::Long; my ($inputfile, $logfile); GetOptions ( "h|help"=> \$help, "f=s" => \$inputfile, "l=s" => \$logfile ) or usage(); sub usage { print ("Invalid Command!! \n"); print ("USAGE: $0 -l <log_file> -f <csv_input_file> \n"); exit; } open (CONFIG, "$inputfile"); while ($line = <CONFIG>) { chomp($line); $line =~ s/"//g; next if $line =~ /^FromName/; @data = split(/\,/, $line); $FromName = $data[0]; $FromMail = $data[1]; $ToName = $data[2]; $ToMail = $data[3]; $CCName = $data[4]; $CCMail = $data[5]; $BCCName = $data[6]; $BCCMail = $data[7]; $ReplyName = $data[8]; $ReplyMail = $data[9]; $Subject = $data[10]; $ReplyMail = $data[9]; $Subject = $data[10]; $TextFile = $data[11]; $HtmlFile = $data[12]; $ReturnName = $data[13]; $ReturnMail = $data[14]; $ReturnReceipt = $data[15]; $ReadReceipt = $data[16]; $Attachment = $data[17]; $blank1 = $data[18]; $blank2 = $data[19]; $AddPdfFile = $data[20]; open (LOG,">> $logfile"); print (LOG localtime(time) . ",$email_id,$attach,$cust_name,$s +date,$ddate\n"); $msg = MIME::Lite->new( To =>$ToEmail, From =>$FromName, Subject =>$Subject, Type =>'multipart/related' ); $msg->attach( ############################## # Called HTML script here... # ############################## ); $msg->attach( Type => 'image/gif', Id => "$Attachment", Path => "$Attachment" ); $msg->send(); close(LOG); } close(CONFIG);

My question is how could i read and open the HTML script file (sample.html) from readHTML.pl script.

Thank you,

Replies are listed 'Best First'.
Re: How to process html script with perl
by NetWallah (Canon) on Oct 30, 2012 at 02:59 UTC
    Seriously ? You are asking how to open and read a file ? Ok - here goes:
    # Place this code wher you want the contents of the file... {open (my $fh,"<","sample.html" ) or die $!; local $/=undef; <$fh>}

                 "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius

Re: How to process html script with perl
by Anonymous Monk on Oct 30, 2012 at 02:59 UTC

      Hi..

      I have use MIME::Lite package to sent email

      Based on your reply the code will be as below
      $msg = MIME::Lite->new( To =>$ToEmail, From =>$FromName, Subject =>$Subject, Type =>'multipart/related' ); $msg->attach( ############################## # Called HTML script here... # ############################## {open (my $fh,"<","sample.html" ) or die $!; local $/ +=undef; <$fh>} ); $msg->attach( Type => 'image/gif', Id => "$Attachment", Path => "$Attachment" );

      is it correct ?

        is it correct ?
        Not quite.

        First of all, I feel it needs to be said that HTML is not a scripting language. It is a markup language.

        Next, your open() statement will never work. Why? Because you can't put it in a hashref like that, and still get your expected result. You may also want to see the documentation for MIME::Lite.
        Instead, use something like the following:

        $msg->attach( Type => "text/html", Id => "sample.html", Path => "/path/to/sample.html" );

        ...which should save you some hassle with the files.

        I hope that solves(or at least helps) your question, although I do highly advise you to read perlintro, among others.

        ~Thomas~ 
        "Excuse me for butting in, but I'm interrupt-driven..."
Re: How to process html script with perl
by sundialsvc4 (Abbot) on Oct 30, 2012 at 14:04 UTC

    At first, I thought that you wanted to parse an HTML file as an input, to get information out of it.   For that purpose, I would have suggested that you search for "html parse" at http://search.cpan.org.

    But instead I think that you may want to generate a customized HTML content using the file shown above as a template.   There are numerous templating engines available in Perl.   My personal favorite happens to be Template::Toolkit.   Although it is most often used in interactive web-sites, it can be used very well in situations like this, especially when you would like to vary what is produced, e.g.: "Thank you for your payment! :-)" vs. "Pay Up Now, You Scum, Or Your Parakeet Gets It! :-[" depending on the balance.

    Start with this tutorial, which deals specifically with how to generate a file (or a string) from a templated source:   Template::Tutorial::Datafile.