sendmail 包含 html 和文件内容

sendmail 包含 html 和文件内容

我需要通过电子邮件发送文件的内容,并在文件的开头和结尾添加一些 html 注释。我已使用“sendmail”来达到此目的,但是当我收到电子邮件时,我没有看到 Outlook 中的换行符造成问题。我尝试过使用“mail”命令,但不确定如何向其中添加 HTML 注释。这是我的片段:

open(my $sendmail,"|/usr/sbin/sendmail -t");
print $sendmail "From: linux\@test.com\n";
print $sendmail "To: kris\@test.com\n";
print $sendmail "Content-Type: text/html\n";
print $sendmail "Subject: My Test\n";
print $sendmail "\n";
my $Header = << 'END';
    <html>
            <head>
                    <h3><font color="red">For test</font></h3>
            </head>
            <body>
                    <h3><font color="red">For test</font></h3>
                    <br/>
            </body>
    </html>
END


print $sendmail $Header;    
print $sendmail "<br>Ran for time:  \n";
my $alert3 = `/bin/cat /home/kris/test.txt`;
print $sendmail $alert3;


close($sendmail);

虽然这对于发送电子邮件来说效果很好,但“alert3”中的文件内容是一行,而当您执行 cat 时,它是几行。我还更改了 Outlook 中的换行符选项,但没有成功。有没有更好的方法来处理这些情况?

答案1

|/usr/sbin/sendmail -t.如果消息被写成一行,将会截断消息,以及其他问题。此外,还明显缺乏错误检查和其他问题(open可能会失败,分叉cat是读取文件内容的最昂贵、最复杂且容易出错的方法)。

对于现代 Perl,人们可能会使用电子邮件::填充物这极大地简化了通过以下方式正确构建有效 MIME 部分的任务电子邮件::MIME。让我们安装它...

$ cpanm Email::Stuffer
...
12 distributions installed
$ 

我有应用程序::cpanminus本地::lib设置;如果需要的话,还有其他方法可以处理模块,例如供应商包,纸盒,等等。

现在,发送代码将类似于

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

use Email::Stuffer;

my $the_html = <<'END';
<html><body><h3><font color="red">test</font></h3><br/></body></html>
END

Email::Stuffer
  ->from('[email protected]')
  ->to('[email protected]')
  ->subject('test test')
  ->html_body($the_html)
  ->attach_file('/home/kris/test.txt')
  ->send;

如果内容/home/kris/test.txt实际上需要混合到 HTML 中间而不是作为不同的文件附加,那么 HTML 相关代码可能看起来像这样(是的,更多模块......)

use Email::Stuffer;
use File::Slurper 'read_text';

my $the_html = <<'HEAD';
<html><body><h3><font color="red">test</font></h3><br/>
HEAD

$the_html .= read_text('/home/kris/test.txt');

$the_html .= <<'TAIL';
</body></html>
TAIL

Email::Stuffer
  ->from('[email protected]')
  ->to('[email protected]')
  ->subject('test test')
  ->html_body($the_html)
  ->send;

不过,如果您想要的不仅仅是将字符串粘在一起并希望 HTML 能够正常工作,那么有些 HTML 模板模块可以包含文件以及其他功能。

答案2

这将是 Outlook 的渲染忽略换行符,而不是您的脚本不发送换行符,但我怀疑 Outlook 的行为确实可能在这里出错:html 忽略换行符,并且您已将内容标记为 html。使用正确的多部分邮件消息的多用途 Internet 邮件扩展标记。这是链接页面中的示例:

作为一个非常简单的示例,以下多部分消息有两个部分,都是纯文本,其中之一是显式键入的,另一部分是隐式键入的:

 From: Nathaniel Borenstein <[email protected]> 
 To:  Ned Freed <[email protected]> 
 Subject: Sample message 
 MIME-Version: 1.0 
 Content-type: multipart/mixed; boundary="simple 
 boundary" 

 This is the preamble.  It is to be ignored, though it 
 is a handy place for mail composers to include an 
 explanatory note to non-MIME compliant readers. 
 --simple boundary 

 This is implicitly typed plain ASCII text. 
 It does NOT end with a linebreak. 
 --simple boundary 
 Content-type: text/plain; charset=us-ascii 

 This is explicitly typed plain ASCII text. 
 It DOES end with a linebreak. 

 --simple boundary-- 
 This is the epilogue.  It is also to be ignored.

明确允许在另一个多部分实体的正文部分中使用多部分的内容类型。在这种情况下,出于明显的原因,必须注意确保每个嵌套多部分实体必须使用不同的边界分隔符。有关嵌套多部分实体的示例,请参阅附录 C。

因此,将 html 部分放在 text/html 部分中,将纯文本放在 text/plain 部分中。

相关内容