我想问一下如何使用Perl脚本更改电子邮件中的单词字体(从Calibri到Courier New)?
我试图将包含表格格式的file.txt发送到电子邮件。 但是,该表在电子邮件内容中的排列不正确,但是file.txt排列得很好。
我在电子邮件中的搜索结果
hello | morning | 30 | 40 |
Yes|evening| 30 | 50 |
电子邮件中的预期输出:
hello | morning | 30 | 40 |
Yes | evening | 30 | 50 |
enter code here
下面是我的代码:
#!/ usr / bin / perl
my $file = '/nfs/site/disks/fm8_pnr_9/users/eewongon/max_cap/vio_table.txt';
open my $fh, '<', $file or die "Cannot open '$file' for reading: $!";
my $text = do {
local $/;
<$fh>
};
close $fh or die "Ugh: $!\n";
$to = 'someone@somewhere.com';
$from = 'someone@somewhere.com';
$subject = 'Testing';
$message = 'Hi, this is email sent by Perl Script';
open(MAIL, "|/usr/sbin/sendmail -t");
# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email Body
print MAIL "$message\n";
print MAIL "\n$text\n";
close(MAIL);
print "Email Sent Successfully\n";
答案 0 :(得分:3)
您必须发送HTML电子邮件才能使用其他字体。但是,建议您使用pre
标签来允许系统选择可用的固定宽度字体,而不是指定字体名称,因为只需要保留基于文本的表格式即可。
# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n";
print MAIL "Content-Type: text/html\n\n";
# Email Body
print MAIL "<html>\n<body>\n<p>$message</p>\n<pre>$text</pre>\n</body>\n</html>\n";
答案 1 :(得分:0)
您已经以表格格式获取了一些数据,因此您可以将其转换为HTML表格,该表格将以电子邮件客户端使用的任何字体正确呈现。对于不支持HTML的电子邮件客户端,请提供纯文本版本,并以固定宽度的字体呈现。
下面的代码解析您的管道分隔的CSV文件,并使用该文件来构建HTML表和纯文本替代文件。然后将两者都添加到MIME::Entity
对象中,该对象将创建您需要能够正确发送的所有标头。
我使用过Text::CSV
和MIME::Entity
,但也存在其他也可以完成其工作的Perl模块。
use strict;
use MIME::Entity;
use Text::CSV;
my $file="test_input.txt"; # Change this to where your file is
my $subject="An email";
my $from="someone\@somewhere.com";
my $to="someone\@somewhere.com";
# Create the CSV parser. Confusingly the "allow_whitespace" strips whitespace rather than allowing it to pad out fields
my $csv=Text::CSV->new({sep_char => "|", allow_whitespace => 1});
# Build the MIME::Entity object
my $mime_email=MIME::Entity->build(
From => $from,
To => $to,
Subject => $subject,
Type => "multipart/alternative");
my $html="<table>\n";
my $text;
if(open(my $fh,"<",$file)) # Use the modern way of opening a file
{
while(my $line = <$fh>)
{
$text .= $line;
$csv->parse($line);
$html .= "<tr>";
foreach my $field ($csv->fields())
{
$html .= "<td>".$field."</td>";
}
$html .= "</tr>\n";
}
close($fh);
}
$html .= "</table>\n";
$mime_email->attach(Type => "text/plain",Data => $text);
$mime_email->attach(Type => "text/html",Data => $html);
# Send the email
if(open(my $mail,"|-","/usr/sbin/sendmail -t"))
{
$mime_email->print($mail);
close($mail);
}
这将生成一封电子邮件,看起来像这样,可以在各种电子邮件客户端上以其首选字体很好地呈现表格。
Content-Type: multipart/alternative; boundary="----------=_1529920158-30125-0"
Content-Transfer-Encoding: binary
MIME-Version: 1.0
X-Mailer: MIME-tools 5.505 (Entity 5.505)
From: someone@somewhere.com
To: someone@somewhere.com
Subject: An email
This is a multi-part message in MIME format...
------------=_1529920158-30125-0
Content-Type: text/plain
Content-Disposition: inline
Content-Transfer-Encoding: binary
hello | morning | 30 | 40 |
Yes | evening | 30 | 50 |
------------=_1529920158-30125-0
Content-Type: text/html
Content-Disposition: inline
Content-Transfer-Encoding: binary
<table>
<tr><td>hello</td><td>morning</td><td>30</td><td>40</td><td></td></tr>
<tr><td>Yes</td><td>evening</td><td>30</td><td>50</td><td></td></tr>
</table>
------------=_1529920158-30125-0--