简体   繁体   English

从Perl脚本发送带有动态表的邮件

[英]Sending a mail with a dynamic table from perl script

I have the following code in Perl: 我在Perl中有以下代码:

foreach my $result ( @results ) {
    if ( $result->{Error} ) {
        print"No response received \n";}  
    else {
        my $H =  "$result->{H}";
        my $I = "$result->{I}";
        $mailbody.=qq(<h4 style="background: blue; color: white;">$H--->$I</h4>);

     } 
}

Here, I am using Mime::Lite to send mails: 在这里,我使用Mime :: Lite发送邮件:

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

$msg->attr("content-type" => "text/html/css");
$msg->send;

What I want is that the result data ie $H and $I to be represented in the form of a table in the mail. 我想要的是结果数据(即$ H和$ I)以邮件中的表的形式表示。

H  |  I
1  |  46
2  | 565756756767
3  | 232132

The number of rows of the table are dynamic and depend on the input given by the user. 该表的行数是动态的,并且取决于用户给出的输入。 How can I do this? 我怎样才能做到这一点?

If you want it to be a table in the email, you should create a table in the email body something like this: 如果希望它成为电子邮件中的表格,则应在电子邮件正文中创建一个表格,如下所示:

$mailbody . = '<table>';
foreach my $result ( @results ) {
    if ( $result->{Error} ) {
        print"No response received \n";}  
    else {
        my $H =  "$result->{H}";
        my $I = "$result->{I}";
        $mailbody.=qq(<tr><td>$H</td><td>$I</td></tr>);

     } 
}
$mailbody . = '</table>';

If you want an HTML table in your email, then add HTML table elements to your output. 如果要在电子邮件中使用HTML表,则将HTML表元素添加到输出中。

# Note: border=1 attribute to make the table borders visible.
$mailbody .= '<table border="1">';

foreach my $result ( @results ) {
    if ( $result->{Error} ) {
        print"No response received \n";}  
    else {
        $mailbody .= qq(<tr><td>$result->{H}</td>)
                   . qq(<td>$result->{I}</td></tr>);

    } 
}

$mailbody .= </table>

In a comment to another answer that suggested something similar, you said that this doesn't work because you can't see the table borders. 在对另一个答案提出类似建议的评论中,您说这行不通,因为看不到表格边框。 That was, of course, a simple case of adding border=1 so that the borders are displayed. 当然,那是添加border=1以便显示border=1的简单情况。

However. 然而。

It's always worth repeating that putting raw HTML strings into your program code is a terrible idea. 始终值得重复一遍,将原始HTML字符串放入程序代码中是一个糟糕的主意。 It's a recipe for an unmaintainable mess. 这是无法维持的混乱局面。 It's a bad idea when creating web applications and it's a bad idea when creating HTML to go into email bodies. 创建Web应用程序时是一个坏主意,而创建HTML进入电子邮件正文时则是一个坏主意。

Far better to separate the code from creating the output and the best way to do that is to use a templating engine like the Template Toolkit . 将代码与创建输出区分开来更好,而做到这一点的最佳方法是使用模板引擎(例如Template Toolkit) By creating a template file that contains all of the HTML output, you make it easier to change the way that the HTML looks without getting bogged down in the Perl code. 通过创建包含所有HTML输出的模板文件,您可以更轻松地更改HTML的外观,而不会陷入Perl代码中。

Also (and I've suggested this to you before) I would suggest that you avoid using MIME::Lite. 另外(并且我之前已经向您建议过)我建议您避免使用MIME :: Lite。 But don't take my word for it. 但是不要相信我。 The current documentation for the module says this: 模块当前文档说明

MIME::Lite is not recommended by its current maintainer. 当前维护者不建议使用MIME :: Lite。 There are a number of alternatives, like Email::MIME or MIME::Entity and Email::Sender, which you should probably use instead. 有许多替代方法,例如Email :: MIME或MIME :: Entity和Email :: Sender,您可能应该改用它们。 MIME::Lite continues to accrue weird bug reports, and it is not receiving a large amount of refactoring due to the availability of better alternatives. MIME :: Lite继续产生怪异的错误报告,由于存在更好的替代方案,因此它没有收到大量的重构。 Please consider using something else. 请考虑使用其他东西。

I recommend switching to Email::Sender (together with Email::MIME ) or Email::Stuffer . 我建议切换到Email :: Sender (以及Email :: MIME )或Email :: Stuffer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM