use Encode qw(encode decode); my $mime_str = encode("MIME-Header", "Sample:Text \N{U+263A}"); # $mime_str is "=?UTF-8?B?U2FtcGxlOlRleHQg4pi6?=" my $mime_q_str = encode("MIME-Q", "Sample:Text \N{U+263A}"); # $mime_q_str is "=?UTF-8?Q?Sample=3AText_=E2=98=BA?=" my $str = decode("MIME-Header", "=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\r\n " . "=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=" ); # $str is "If you can read this you understand the example." use Encode qw(decode :fallbacks); use Encode::MIME::Header; local $Encode::MIME::Header::STRICT_DECODE = 1; my $strict_string = decode("MIME-Header", $mime_string, FB_CROAK); # use strict decoding and croak on errors
Encode method takes a scalar string as its input and uses strict UTF-8 encoder for encoding it to UTF-8 bytes. Then a sequence of UTF-8 bytes is encoded into MIME encoded-words ("MIME-Header" and "MIME-B" use a Base64 variant while "MIME-Q" uses a Quoted-Printable variant) where each MIME encoded-word is limited to 75 characters. MIME encoded-words are separated by "CRLF SPACE" and joined to one output string. Output string is suitable for unstructured field body of the email header.
Both encode and decode methods propagate CHECK flags when encoding and decoding the MIME charset.
Versions prior to 2.24 (part of Encode 2.87) ignored CHECK flags. The MIME encoder used not strict utf8 encoder for input Unicode strings which could lead to invalid UTF-8 sequences. MIME decoder used also not strict utf8 decoder and additionally called the decode method with a "Encode::FB_PERLQQ" flag (thus user-specified CHECK flags were ignored). Moreover, it automatically croaked when a MIME encoded-word contained unknown encoding. Since version 2.24, this module uses strict UTF-8 encoder and decoder. And CHECK flags are correctly propagated.
Since version 2.22 (part of Encode 2.83), the MIME encoder should be fully compliant to RFC 2047 <https://tools.ietf.org/html/rfc2047> and RFC 2231 <https://tools.ietf.org/html/rfc2231>. Due to the aforementioned bugs in previous versions of the MIME encoder, there is a less strict compatible mode for the MIME decoder which is used by default. It should be able to decode MIME encoded-words encoded by pre 2.22 versions of this module. However, note that this is not correct according to RFC 2047 <https://tools.ietf.org/html/rfc2047>.
In default not strict mode the MIME decoder attempts to decode every substring which looks like a MIME encoded-word. Therefore, the MIME encoded-words do not need to be separated by white space. To enforce a correct strict mode, set variable $Encode::MIME::Header::STRICT_DECODE to 1 e.g. by localizing:
use Encode::MIME::Header; local $Encode::MIME::Header::STRICT_DECODE = 1;