我试图以表格格式对齐两个数组的内容并发送电子邮件。但表中的第二列并未按需要对齐。第二列的内容显示为单行。
我也附加了输出表:
#!/usr/bin/perl
use strict;
use warnings;
use MIME::Lite;
use HTML::Entities;
my $msg;
my @Sucess = qw(s1 s2 s3 s4 s5 s6);
my @Failed = qw(f1 f2 f3 f4);
my $html = '<table style="width:600px;margin:0 100px" border="1" BORDERCOLOR="#000000">
<thead><th bgcolor="#9fc0fb">Successful</th><th bgcolor="#9fc0fb">Failed</th></thead>
<tbody>';
$html .= "<tr><td>$_</td>" for @Sucess;
$html .= "<td>$_</td>" for @Failed;
$html .= " </tr>";
$msg = MIME::Lite->new(
from => 'foo@abc.com',
To => 'foo@abc.com',
Subject => 'Status of Update',
Type => 'multipart/related'
);
$msg->attach(
Type => 'text/html',
Data => qq{
<body>
<html>$html</html>
</body>
},
);
MIME::Lite->send ('smtp','xyz.global.abc.com' );
$msg->send;
答案 0 :(得分:2)
您需要使用按逻辑顺序工作的内容替换构建表的代码。需要逐行定义HTML表。你不能处理所有的成功,然后处理所有的失败。
我用这样的代码替换你的代码的中间部分:
use List::Util qw[max];
my $max = max($#Sucess, $#Failed);
for (0 .. $max) {
$html .= '<tr><td>';
$html .= $Sucess[$_] // '';
$html .= '</td><td>';
$html .= $Failed[$_] // '';
$html .= "</td></tr>\n";
}
但实际上,我绝不会将原始HTML放在Perl程序中。请改用templating system。
答案 1 :(得分:1)
首先,您遍历@Sucess
中的每个项目以及每个项目:
$html .= "<tr><td>$_</td>" for @Sucess;
然后,您查看@Failed
中的每个项目以及每个项目:
@Sucess
)$html .= "<td>$_</td>" for @Failed;
最后,您明确关闭您创建的最后一个表行:
$html .= " </tr>";
要获得所需的布局(明显非表格式),您需要逐行工作。您无法处理所有@Sucess
的所有@Failed
和然后。
my @Sucess= qw(s1 s2 s3 s4 s5 s6);
my @Failed= qw(f1 f2 f3 f4);
my $html = "<table>\n";
do {
my $s = shift @Sucess;
my $f = shift @Failed;
$html .= sprintf("<tr> <td> %s </td> <td> %s </td> </tr> \n", map { $_ // '' } $s, $f);
} while ( @Sucess or @Failed );
$html .= "</table>";
print $html;