【问题标题】:Perl/MIME encoded text troublePerl/MIME 编码文本问题
【发布时间】:2011-04-30 21:43:07
【问题描述】:

我有一个 MIME 编码的消息(在 Maildir 中),它具有两个 base64 编码的标头(通过潜伏相关问题解决(Decode an UTF8 email header),解码('MIME-Header',$val),主体在纯文本和正文中的文本/纯 base64 编码数据中;

据说base64数据是用utf-8编码的。 但是,当我这样做时:

use MIME::Base4;
..
$decoded = decode_base64($block_from_line_array); # MIME body extracted from message's
$msgtext .= $decoded;
..
print decode('utf-8', $msgtext);

该块似乎解码错误。

print decode('utf-8', $msgtext); works ok, when message body is in utf-8

附上:

X-Priority: 3
X-Mailer: PHPMailer (phpmailer.sourceforge.net) [version 2.0.4]
X-Mailer: http://www.we.praise.buggy.php.scripts.what.we.do.when.we.dont.do.us
X-MessageID: 140
MIME-Version: 1.0
Content-Type: multipart/alternative;
 boundary="b1_16819d4d69564bfc0185ed5b9508ad31"

<Here the body begins -- mhambra>
--b1_16819d4d69564bfc0185ed5b9508ad31
Content-Type: text/plain; charset = "utf-8"
Content-Transfer-Encoding: base64
<MIME BLOCK>

已知邮件可以在 GMail 中正确显示。

【问题讨论】:

    标签: perl mime


    【解决方案1】:

    在我正在测试的一些示例电子邮件中,FWIW Email::MIME 对我来说似乎很脆弱。 Email::Parser 对我来说效果更好,试图提取可能在 base64 中的 text/html 或 text/plain 段(并且通常具有传输编码 base64 / utf-8

    use MIME::Parser;
    use MIME::Base64;
    
    sub flatten_parts {
        my ($mimePart, $fh) = @_;
        $fh or $fh = select;
        my $part;
        no strict 'refs';
        if($mimePart->mime_type =~ /text\/(plain|html)/i){
            my $base64=join('',@{$mimePart->body}); # This will be the base64 we're after.
            my $encoding = $mimePart->head->mime_encoding;
            if($encoding eq 'base64'){
                    my $plainContent=MIME::Base64::decode_base64($base64);
                    print $plainContent;
            }
        }   
        ### walk the parts:
        my @parts = $mimePart->parts;
        foreach $part (@parts) {
            flatten_parts($part, $fh);
        }
    }
    ### Create a new parser object:
    our $parser = new MIME::Parser;
    ### Parse an input filehandle:
    $entity = $parser->parse(\*STDIN);
    flatten_parts($entity);
    

    【讨论】:

      【解决方案2】:

      使用像Email::MIME 这样的模块来完成这项艰巨的工作。

      use strict;
      use warnings;
      use Email::MIME;
      
      my $msg = Email::MIME->new($message_text);
      print $msg->body;
      

      【讨论】:

        【解决方案3】:

        遗憾的是,如果您碰巧在 UTF8 格式的邮件中写了一个希腊 lambda,MIME::Parser barfs,并且似乎没有已知的解决方法 — 甚至不编辑所有 *.pm 文件,添加 use utf8; 等。

        尝试在 Thunderbird 中打开一条新消息,附加一些文件,在正文中写入该 unicode 字符并将其保存为 .eml 文件。 MIME::Parser 会抛出错误。

        另见CPAN Bug #105377

        【讨论】:

          猜你喜欢
          • 2014-05-24
          • 1970-01-01
          • 1970-01-01
          • 2014-06-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-14
          相关资源
          最近更新 更多