I am writing a simple imap client with Perl's Net::IMAP::Simple module. I'd like to print subjects of messages on standart output. The subject is encoded in Quoted-Printables, so I have to decode it with MIME::QuotedPrint decode_qp() function. Everything is printed fine, except whitespaces, they remain encoded and I have no idea why. The output looks like this now:
[073] =?UTF-8?Q?[Myawesome_subject_topic]?= =?UTF-8?Q?_Сообщение?= =?UTF-8?Q?_номер?=
As you can see, whitespaces are locaed between ?= and =?UTF-8?Q?_ 'tags'. Not sure how to deal with them. The code for the relevant part is below
my $nm = $imap->select('INBOX');
for (my $i = 1; $i <= $nm; $i++) {
if ($imap->seen($i)) {
print '*';
}
else {
print " ";
}
my $es = Email::Simple->new(join '', @{ $imap->top($i) } );
my $decoded = $es->header('Subject');
$decoded = decode_qp($decoded);
printf("[%03d] %s\n", $i, $decoded);
}
UPDATE AND SOLUTION
Use Encode module instead of MIME::QuotedPrint
use Encode qw(decode);
Decode subject like this
$decoded = decode("MIME-Header", $encoded);
additional info on the topic in the accepted answer below