为什么 Email::Stuffer base64-encode 与 MIME::Base64 不同,utf8 如何适应?

Why does Email::Stuffer base64-encode differently than MIME::Base64 and how does utf8 fit in?

提问人:Robert 提问时间:10/8/2023 更新时间:10/9/2023 访问量:65

问:

我想发送一封简单的电子邮件。正如预期的那样,它将带有非 ASCII 字符的标头编码为编码字。但是当我解码它们时(无论是在我的邮件客户端还是在Perl中),我得到不同的文本,并且以不同的方式对相同的文本进行编码。Email::StufferMIME::Base64

use strict;
use warnings;
use Email::Stuffer;
use MIME::Base64;

my $text = 'Ümläut';
print "$text in base64: ", encode_base64($text, ''), "\n";
print "and back: ", decode_base64(encode_base64($text)), "\n";

my $stuffer = Email::Stuffer->subject($text);
my $dump = $stuffer->as_string();
print "Mail dump:\n---\n$dump\n---\n";

$dump =~ m{^Subject:\s*=\?UTF-8\?B\?(.+)\?=}m;
my $encoded = $1;
print "in Subject: $encoded\n";
my $decoded = decode_base64($encoded);
print "subject decoded: $decoded\n";

这打印:

Ümläut in base64: w5xtbMOkdXQ=
and back: Ümläut
Mail dump:
---
Date: Sat, 7 Oct 2023 16:31:59 -0500
MIME-Version: 1.0
Subject: =?UTF-8?B?w4PCnG1sw4PCpHV0?=


---
in Subject: w4PCnG1sw4PCpHV0
subject decoded: Ãmläut

(echo "Ümläut" | base64在 shell 上同意并打印 w5xtbMOkdXQK)MIME::Base64

程序源代码在 utf8 中。当我添加 after 时,第一个 s 不会打印预期的变音符号,但会按预期工作。use utf8;use warnings;printEmail::Stuffer

�ml�ut in base64: 3G1s5HV0
and back: �ml�ut
Mail dump:
---
Date: Sat, 7 Oct 2023 16:32:50 -0500
MIME-Version: 1.0
Subject: =?UTF-8?B?w5xtbMOkdXQ=?=


---
in Subject: w5xtbMOkdXQ=
subject decoded: Ümläut

这里有什么区别/为什么会发生这种情况,我怎样才能同时获得两者并达成一致?MIME::Base64Email::Stuffer

perl 电子邮件 utf-8 base64

评论


答:

4赞 ikegami 10/8/2023 #1

->subject需要文本(解码文本,Unicode 码位字符串)。

encode_base64需要字节(例如使用 UTF-8 编码的文本)。

固定:

use strict;
use warnings;
use feature qw( say );

use utf8;                               # Source code is encoded using UTF-8.
use open ':std', ':encoding(UTF-8)';    # Terminal expects/provides UTF-8.

use Email::Stuffer qw( );
use Encode         qw( decode encode );
use MIME::Base64   qw( decode_base64 encode_base64 );

my $text_ucp = 'Ümläut';                # String of Unicode Code Points.
say $text_ucp;                          # Ümläut

my $text_utf8_base64 = encode_base64( encode( "UTF-8", $text_ucp ), '');
say $text_utf8_base64;                  # w5xtbMOkdXQ=

my $roundtrip_ucp = decode( "UTF-8", decode_base64( $text_utf8_base64 ) );
say $roundtrip_ucp;                     # Ümläut

my $stuffer = Email::Stuffer->subject( $text_ucp );
print $stuffer->as_string();            # =?UTF-8?B?w5xtbMOkdXQ=?=