perl:HTML::Mason-以表格格式在HTML标记中打印一个perl散列



我正试图发送一封关于哈希值的html电子邮件(报告)。但我无法以表格格式打印哈希值。我使用HTML::Mason来执行我的perl命令(通过散列循环),并将其打印在报告的末尾。但是我的perl代码没有被执行。

use HTML::Entities;
use Mail::Sendmail;
use MIME::Lite;
use Term::ANSIColor;
use Time::localtime;
use HTML::Mason;
my $ti = localtime;
my ( $day, $month, $year ) = ( $ti->mday, $ti->fullmonth, $ti->year );
# DEFINE A HASH
%coins = ( "Quarter" => 25, "Dime" => 10, "Nickel" => 5 );
$html = <<END_HTML;
   Please direct any questions to <a href="mailto:abc@mydomain.com">MyEmailID</a><br><br>
  <table border='1'>
   <th>Keys</th><th>Values</th>
% while (($key, $value) = each(%coins)){       
   <TR>     
   <TD><% $key %></TD>      
   <TD><% $value %></TD>      
   </TR>       
% }
  </table>;
END_HTML
$msg = MIME::Lite->new(
    from    => 'abc@mydomain.com',
    To      => 'def@mydomain.com',
    Subject => 'Report',
    Type    => 'multipart/related'
);
$msg->attach(
    Type => 'text/html',
    Data => qq{
        <body>
        <html>$html</html>
        </body>
    },
);
MIME::Lite->send( 'smtp', 'xxxs.xxxx.xxxxx.com' );
$msg->send;

由于您只想生成一个表,因此无需使用HTML::Mason:即可轻松完成此操作

use HTML::Entities;
use Mail::Sendmail;
use MIME::Lite;
use Term::ANSIColor;
use Time::localtime;
my $ti = localtime;
my ( $day, $month, $year ) = ( $ti->mday, $ti->fullmonth, $ti->year );
# DEFINE A HASH
my %coins = ( "Quarter" => 25, "Dime" => 10, "Nickel" => 5 );
my $html = '<table border="1">
<thead><th>Keys</th><th>Values</th></thead>
<tbody>';
while ( my($key, $value) = each(%coins)) {
    $html .= "<tr><td>$key</td><td>$value</td></tr>";
}
$html .= "</tbody></table>";
my $greeting = 'Please direct any questions to <a href="mailto:abc@mydomain.com">MyEmailID</a><br><br>';
my $outtro = '<br><br>See yas later, alligator!';
$html = $greeting . $html . $outtro;
$msg = MIME::Lite->new(
    from    => 'abc@mydomain.com',
    To      => 'def@mydomain.com',
    Subject => 'Report',
    Type    => 'multipart/related'
);
$msg->attach(
    Type => 'text/html',
    Data => qq{
        <body>
        <html>$html</html>
        </body>
    },
);
MIME::Lite->send( 'smtp', 'xxxs.xxxx.xxxxx.com' );
$msg->send;

除非您的电子邮件需要大量的复杂性,否则可能不值得为此目的使用HTML::Mason。如果你要使用HTML::Mason来处理电子邮件,你需要设置你的脚本来调用它——有关详细信息,请参阅pod。不幸的是,您不能仅仅将Mason命令嵌入到字符串中。

类似的东西?

# DEFINE A HASH
my %coins = ( "Quarter" => 25,
              "Dime" => 10,
              "Nickel" => 5, );
for my $no (keys %coins) {
    print qq(
    <table>
        <tr><td> $no </td></tr>
        <tr><td> $coins{$no} </td></tr>
    </table>
    );
}

相关内容

  • 没有找到相关文章

最新更新