【问题标题】:MIME::Entity headers encoding correct?MIME::Entity 标头编码正确吗?
【发布时间】:2011-10-17 08:49:27
【问题描述】:

我在 Perl 中使用 MIME::Entity 模块来创建 MIME 消息。一些标头似乎编码正常,而其他标头似乎有折叠问题。

代码:

use strict;
use Encode;
use MIME::Entity;

my %build_params = (
    'Charset'    => 'UTF-8',
    'From'       => encode('MIME-Header', 'Fantasy Email <vujerldujhgurtelhwgutrwhgunwlhvulhgvnuwlhvuwlnhvgnulwh@gmail.com>'),
    'Subject'    => encode('MIME-Header', "A very long subject that will span on multiple lines in the headers, with a leading sp\
ace at the beginning of each new line."),
    'Type'       => 'multipart/alternative',
);


my $top = MIME::Entity->build(%build_params);

$top->print_header();

输出:

Content-Type: multipart/alternative;
 boundary="----------=_1312196104-11708-0";
 charset="UTF-8"
Content-Transfer-Encoding: binary
MIME-Version: 1.0
X-Mailer: MIME-tools 5.427 (Entity 5.427)
Subject: A very long subject that will span on multiple lines in the
 headers,  with a leading space at the beginning of each new line.
From: Fantasy Email
 <vujerldujhgurtelhwgutrwhgunwlhvulhgvnuwlhvuwlnhvgnulwh@gmail .com>

Subject 似乎被正确拆分为多行。 From 没有,在com 之前留了一个空格,但换行符消失了。

这是标准行为还是我在 MIME::Entity 中发现了错误?

【问题讨论】:

  • 对我来说似乎是一个错误。主要是.com 之前的空格。此外,如果您完全复制它产生的内容,似乎在headers, 之后添加了一个新空格。
  • 嗯,逗号后面的双空格是对的。这显然是一个错误。
  • 另请注意,添加的空格可能在两个标题中的位置相同,因此可能是MIME::Entity上的拆分表达式中的错误。

标签: perl email encoding header mime


【解决方案1】:

Encode::MIME::Header(称为encode('MIME-Header', ...))会进行一些行拆分(在RFC 822 中称为折叠)。

不幸的是,MIME::Entity 也进行了一些行拆分,可能以不同的方式。它还摆脱了由 Encode::MIME::Header 生成的换行符。但它会留下空间。

我很乐意让 MIME::Entity 处理我的标头编码,但看起来它只是做了行分割部分。所以我想我还是得自己编码。

作为一种解决方法,我使用

从我的编码标题中删除了行分割标记
 my $encoded_from = encode('MIME-Header', 'Fantasy Email <vujerldujhgurtelhwgutrwhgunwlhvulhgvnuwlhvuwlnhvgnulwh@gmail.com>');
 $encoded_from =~ s/\r?\n\s//g;

(主题也一样。)

现在输出如下所示:

Subject: A very long subject that will span on multiple lines in the
 headers, with a leading space at the beginning of each new line.
From: Fantasy Email
 <vujerldujhgurtelhwgutrwhgunwlhvulhgvnuwlhvuwlnhvgnulwh@gmail.com>

我想知道是否有更优雅的解决方案,例如具有 MIME::Entity 兼容模式的 Encode::MIME::Header 或类似的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 2016-09-03
    相关资源
    最近更新 更多