【问题标题】:How to change the word font in email using perl如何使用 perl 更改电子邮件中的单词字体
【发布时间】:2018-06-25 03:32:56
【问题描述】:

我想问如何使用 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";

【问题讨论】:

    标签: perl email


    【解决方案1】:

    您必须以 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";
    

    【讨论】:

    • 也许你的表在$message而不是$text?在这种情况下,您也应该将$message 放入pre 标记中。
    • 也许使用style 属性来为pre 标签添加更明确的CSS:&lt;pre style="font-family: Courier New, monospace"&gt; 尽管很难想象为什么您的系统在没有额外的情况下不能正确呈现pre 标签CSS。
    • 我可以将电子邮件中的所有字体转换为 Courier New 吗?
    • 您可以将style="font-family: Courier New, monospace" 属性应用于body 标记,以确保整个电子邮件都使用该字体,尽管您仍然需要pre 标记来保留空格和换行符。
    • 打印邮件 "\n\n

      $message

      \n
      $text\n\n\n";我试过使用这个命令,但它显示语法错误..是我做错了吗?
    【解决方案2】:

    您有一些表格格式的数据,因此您可以将其转换为 HTML 表格,该表格将以电子邮件客户端使用的任何字体正确呈现。对于不支持 HTML 的电子邮件客户端,请提供纯文本版本,该版本将以固定宽度的字体呈现。

    下面的代码会解析以管道分隔的 CSV 文件,并使用它来构建 HTML 表格和纯文本替代方案。然后将两者都添加到MIME::Entity 对象中,该对象将创建您需要能够正确发送它的所有标头。

    我使用过 Text::CSVMIME::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--
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 2013-09-28
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多