MIME 和 Perl

Encode::MIME::Header

Multipurpose Internet Mail Extensions(多用途的网际邮件扩充)

use Encode qw/encode decode/;
$utf8   = decode('MIME-Header', $header);
$header = encode('MIME-Header', $utf8);

This module implements RFC 2047 Mime Header Encoding. There are 3 variant encoding names; MIME-Header , MIME-B and MIME-Q . The difference is described below

            decode()          encode()
----------------------------------------------
MIME-Header Both B and Q      =?UTF-8?B?....?=
MIME-B      B only; Q croaks  =?UTF-8?B?....?=
MIME-Q      Q only; B croaks  =?UTF-8?Q?....?=

代码示例:

use Encode;
use utf8;

my $plain_str = "我爱编程";
my $str = Encode::encode('MIME-Header', $plain_str);
print $str, "\n"; # =?UTF-8?B?5oiR54ix57yW56iL?=

$str = Encode::decode('MIME-Header', $str);
print $str, "\n"; # 我爱编程

代码示例:

use Encode;
use utf8;
use Encode::HanExtra; # 处理gb18030这种编码的, 如果不添加的话, 会报错: "Unknown encoding: gb18030"

my $str = "=?gb18030?B?1tyxqC3A7s6wo6gyMDE2LjEwLjE3o6k=?=";
$str = Encode::decode('MIME-Header', $str);
print $str, "\n";

Perl

这篇文章: 有用 无用
创建 2016-11-14 02:48:44 / 更新 2016-11-14 04:05:47