Andrew Newby
Andrew Newby

Reputation: 5206

Base64 email subject not decoding correctly

I'm trying to see why my base64 email subject isn't decoding. I use this Perl code to create it:

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

Which gives me:

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

If I copy and paste:

w63DsyBEZW1hbmRlIGRlIHJlc2VydmF0aW9uIGR1IHd3dy5jaGFtYnJlc2Rob3Rlcy5vcmcgLSBSZXNlcnZhdGlvbiByZXF1ZXN0IGZyb20gd3d3LmNoYW1icmVzZGhvdGVzLm9yZyBbI10=

...into this site to decode it:

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

..then it comes out fine!

The full email header looks like:

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

This is how it comes out in my email client:

enter image description here

...and this is how I see it when converting it via an online tool:

enter image description here

UPDATE: This is actually how Email::MIME is creating the email:

From: "Andy chambresdhotes.org" <[email protected]>
To: [email protected]
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

The full Perl code I used to make the header, is:

    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",
        }
  );

Upvotes: 1

Views: 3816

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123541

You are using Email::MIME with header_str and then you try to give the encoded subject, i.e. something like this:

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

As documented header_str expects the unencoded (i.e. unicode) string and will encode the string itself. This results in the double encoding you see. To cite from the documentation:

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.

Correct use would instead provide the unencoded string like this:

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

Alternatively you could provide the encoded string by using header:

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

To cite the documentation:

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

Upvotes: 5

Related Questions