MIME::Entity 标头编码正确吗?

发布于 2024-11-27 04:53:48 字数 1452 浏览 4 评论 0 原文

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

代码:

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

my %build_params = (
    'Charset'    => 'UTF-8',
    'From'       => encode('MIME-Header', 'Fantasy Email <[email protected]>'),
    '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 中发现了错误?

I use MIME::Entity module in Perl to create a MIME message. Some of the headers seem to be encoded OK, while other seem to have issues with folding.

Code:

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

my %build_params = (
    'Charset'    => 'UTF-8',
    'From'       => encode('MIME-Header', 'Fantasy Email <[email protected]>'),
    '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();

Output:

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>

The Subject seems to be correctly split into multiple lines. The From doesn't, leaving a space before the com, but the newline is gone.

Is this standard behavior or have I found a bug in MIME::Entity?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黄昏下泛黄的笔记 2024-12-04 04:53:48

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

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

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

作为解决方法,我从编码的标头中删除了行分割标记

 my $encoded_from = encode('MIME-Header', 'Fantasy Email <[email protected]>');
 $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
 <[email protected]>

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

Encode::MIME::Header (called as encode('MIME-Header', ...)) does some line splitting (called folding in the RFC 822).

Unfortunately, MIME::Entity does some line splitting too, probably in a different way. It also gets rid of the newline generated by Encode::MIME::Header. It leaves the spaces though.

I would be happy to leave MIME::Entity deal with the encoding of my headers, but it looks like it just does the line splitting part. So I guess I still have to encode them myself.

As a workaround, I removed the line splitting markers from my encoded headers with

 my $encoded_from = encode('MIME-Header', 'Fantasy Email <[email protected]>');
 $encoded_from =~ s/\r?\n\s//g;

(And same thing for the subject.)

Now the output looks like this:

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
 <[email protected]>

I'm wondering if there's a more elegant solution, like Encode::MIME::Header featuring a MIME::Entity compatibility mode or something like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文