Base64 电子邮件主题未正确解码

Base64 email subject not decoding correctly

我想看看为什么我的 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

..然后结果很好!

完整的电子邮件 header 如下所示:

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

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

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

更新:这实际上是 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

我用来制作 header 的完整 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",
        }
  );

您将 Email::MIMEheader_str 一起使用,然后您尝试提供编码的主题,即类似这样的内容:

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

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

The header_str parameter is a list of headers pairs to include in the message. The value for each pair is expected to be a text string that will be MIME-encoded as needed.

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

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

或者,您可以使用 header:

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

引用文档:

A similar header parameter can be provided in addition to or instead of header_str. Its values will be used verbatim.