2011-08-01 19 views
2

我在Perl中使用MIME :: Entity模塊創建MIME消息。某些標題似乎編碼正常,而其他標題似乎存在摺疊問題。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 
<[email protected] .com> 

Subject似乎正確地分成多行。 From沒有,在com之前留下一個空格,但換行符不見了。

這是標準行爲還是我在MIME :: Entity中發現了一個錯誤?

+0

看起來像一個bug給我。 '.com'之前的空間大部分都是。另外,如果您完全複製了*製作的內容,似乎會在'headers,'後添加一個新空格。 –

+0

嗯,你說的是逗號後的雙倍空間。這一個顯然是一個錯誤。 – jeje

+0

還要注意,添加的空間*可能*在兩個頭文件的相同位置,所以它可能是'MIME :: Entity'上的分割表達式中的一個錯誤。 –

回答

1

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兼容模式或類似的東西。