2

I am working on a project where I need to access CSV file form a web URL. I am able access the file and print the content from CSV file in the terminal, but I'm unable to produce HTML table (then I'll later send email using MIME).

Here is my code - I need complete CSV file as HTML table delivered to my email.

#!/usr/bin/perl
use strict;
use warnings;

use LWP::Simple;
use POSIX qw(strftime);
use MIME::Lite;
use Getopt::Long;

my $to="pankajdustbin\@gmail.com";
my $from="pankajdustbin\@gmail.com";
$subject="CSV File";

my $content = `curl -s "https:csvfile.com"`;

@output = split(/\n/,$content);

foreach my $line (@output) {
    my ($col1, $col2, $col3, $col4, $col5, $col6) = split(/,/, $line);
    #print "\n$col1, $col2, $col3, $col4, $col5, $col6\n";

    $message = "<tr> <td>$col1</td> <td>$col2</td> <td>$col3</td> <td>$col4</td> <td>$col5</td> <td>$col6</td></tr>";
}

my $msg=MIME::Lite->new(
        From => $from,
        To => $to,
        Subject => $subject,
        Data => $message
        );

$msg->attr('content-type' => 'text/html');
#MIME::Lite->send("smtp");
$msg->send;

With this code, the HTML table contains only the last row of the CSV. Can someone help me how I should do?

CSV has around 100 rows, and the sample output that I see in terminal as below:

1.2.3.4  03-04-2022.  03-08-2022. Red.  1%.  Positive
5.2.3.4  03-05-2022.  04-08-2022. Blue.  1%.  Neutral
and so on...
1
  • 1
    Aside from the problem you're asking about, your code suffers from code injection. You'll need to replace <, > and & in the values you insert with appropriate entities. Commented Jun 23, 2022 at 5:54

2 Answers 2

5

The problem is that you overwrite the contents of the $message variable each time through the foreach loop. This means that $message will only have the last value that you assign to it.

You could append to the contents of the variable using the .= operator:

my $message;
foreach my $line (@output) {
    my ($col1, $col2, $col3, $col4, $col5, $col6) = split(/,/, $line);
    $message .= "<tr> <td>$col1</td> <td>$col2</td> <td>$col3</td> <td>$col4</td> <td>$col5</td> <td>$col6</td></tr>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

Previous answer covered that you overwrite $message in the loop what is not you have intended.

Following snippet code demonstrates slightly different approach to build html table utilizing split and for loop.

Then table can be utilized anyway you desire -- send it by mail or generate html page. In this demo code complete html page generated.

Note #1: \n and \t optional and added for html readability only

Note #2: as no sample input CVS file was provided the content was assumed based on provided output in terminal

use strict;
use warnings;
use feature 'say';

my $table = '<table border=1>';

while( my $line = <DATA>) {
    chomp $line;
    $table .= "\n\t\t\t<tr>";
    $table .= "\n\t\t\t\t<td>" . $_ . '</td>' for split(/,/,$line);
    $table .= "\n\t\t\t</tr>";
}

$table .= "\n\t\t</table>";

my $html = 
"<html lang='en'>
    <head>
        <meta charset='utf8' />
        <link rel='stylesheet' href='css/styles.css'>
        <title>
            CVS table
        </title>
    </head>
    <body>
        $table
    </body>
</html>
";

say $html;

__DATA__
1.2.3.4,03-04-2022,03-08-2022,Red,1%,Positive
5.2.3.4,03-05-2022,04-08-2022,Blue,1%,Neutral
1.2.3.4,03-04-2022,03-08-2022,Red,1%,Positive
5.2.3.4,03-05-2022,04-08-2022,Blue,1%,Neutral
1.2.3.4,03-04-2022,03-08-2022,Red,1%,Positive
5.2.3.4,03-05-2022,04-08-2022,Blue,1%,Neutral

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.