Base64电子邮件主题无法正确解码

时间:2018-01-11 14:11:05

标签: perl email base64 email-headers

我试图了解为什么我的 base64 电子邮件主题没有解码。我使用这个 Perl 代码来创建它:

use MIME::Base64;
use Encode;
$subject = "=?UTF-8?B?" . encode_base64(encode("utf8", $subject), "") . "?=";

这给了我:

=?UTF-8?B?w63DsyBEZW1hbmRlIGRlIHJlc2VydmF0aW9uIGR1IHd3dy5jaGFtYnJlc2Rob3Rlcy5vcmcgLSBSZXNlcnZhdGlvbiByZXF1ZXN0IGZyb20gd3d3LmNoYW1icmVzZGhvdGVzLm9yZyBbI10=?=

如果我复制并粘贴:

w63DsyBEZW1hbmRlIGRlIHJlc2VydmF0aW9uIGR1IHd3dy5jaGFtYnJlc2Rob3Rlcy5vcmcgLSBSZXNlcnZhdGlvbiByZXF1ZXN0IGZyb20gd3d3LmNoYW1icmVzZGhvdGVzLm9yZyBbI10=

...进入此网站进行解码:

http://www.webatic.com/run/convert/base64.php

..然后就好了!

完整的电子邮件标题如下:

Subject: =?UTF-8?B?w63DsyBEZW1hbmRlIGRlIHJlc2VydmF0aW9uIGR1IHd3dy5jaGFtYnJlc2Rob3Rlcy5vcmcgLSBSZXNlcnZhdGlvbiByZXF1ZXN0IGZyb20gd3d3LmNoYW1icmVzZGhvdGVzLm9yZyBbI10=?=

这就是我的电子邮件客户端的结果:

enter image description here

...这就是我通过在线工具转换它时的看法:

enter image description here

更新:这实际上是 Email :: MIME 创建电子邮件的方式:

From: "Andy chambresdhotes.org" <test@foo.org>
To: andynewby@bar.com
Subject: =?UTF-8?B?PT9VVEYtOD9CP3c2M0RzeUJFWlcxaGJtUmxJR1JsSUhKbGMyVnlk?=
 =?UTF-8?B?bUYwYVc5dUlHUjFJSGQzZHk1amFHRnRZbkpsYzJSb2IzUmxjeTV2Y21jZ0xT?=
 =?UTF-8?B?QlNaWE5sY25aaGRHbHZiaUJ5WlhGMVpYTjBJR1p5YjIwZ2QzZDNMbU5vWVcx?=
 =?UTF-8?B?aWNtVnpaR2h2ZEdWekxtOXlaeUJiSTEwPT89?=
Date: Thu, 11 Jan 2018 14:46:05 +0000
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="15156819650.22DF651.1990"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

我用来制作标题的完整Perl代码是:

    my $email = Email::MIME->create(
      header_str => [
          From => $from,
          To => [ $to ],
          Subject => $subject,
      ],
      parts => \@parts,
      attributes => {
            encoding => 'base64', # this was the trick
            charset  => "UTF-8",
            content_type => "multipart/alternative",
            disposition  => "inline",
        }
  );

1 个答案:

答案 0 :(得分:4)

您正在Email::MIME使用header_str,然后尝试给出已编码的主题,例如:

my $mail = Email::MIME->create(
    header_str => [
        Subject => '=?UTF-8?B?w4PChHJnZXJsaWNoZSBHcsODwrzDg8KfZQ==?='
    ]
);

由于documented header_str期望未编码(即unicode)字符串并将对字符串本身进行编码。这会导致您看到的双重编码。引用文档:

  

header_str参数是要包含在邮件中的标头对列表。每对的值应为文本字符串,将根据需要进行MIME编码

正确使用会改为提供未编码的字符串,如下所示:

my $mail = Email::MIME->create(
    header_str => [
        Subject => 'Ärgerliche Grüße'
    ]
);

或者,您可以使用header

提供编码的字符串
my $mail = Email::MIME->create(
    header => [
        Subject => '=?UTF-8?B?w4PChHJnZXJsaWNoZSBHcsODwrzDg8KfZQ==?='
    ]
);

引用文档:

  

header之外或代替header_str,可以提供类似的$('input.inputFile').on('change', function() { var val = $(this).val(); $(this).siblings('span').text(val); }) 参数。 其值将逐字使用