Message ID and In-Reply-To encoding issue (=?UTF-8?Q?)

I am using zend mail to send/reply to emails.

I need to create a message-ID and set In-Reply-To. The code works, but when I check the results there is a weird encoding, which messes everything up.

How it looks like:

In-Reply-To: =?UTF-8?Q?<8a299e85f4f6b03a991740b10ed194bde9c21619@localhost.eu>?=
Message-ID: =?UTF-8?Q?<76251b6a8a859d4c2bd7ecd70a3aff811991d6b7@localhost.eu>?=

How it should look:

In-Reply-To: <8a299e85f4f6b03a991740b10ed194bde9c21619@localhost.eu>
Message-ID: <76251b6a8a859d4c2bd7ecd70a3aff811991d6b7@localhost.eu>

The code I use to create the message-ID:

$messageid = new Header\MessageId();

$messageid->setEncoding("UTF-8");

$_messageid = $messageid->createMessageId();

if (strpos($_messageid, "?UTF-8?")) {

    $_messageid = mb_decode_mimeheader($_messageid);

} 

$message->getHeaders()->addHeaderLine('Message-ID', '<' . $_messageid . '>');

I also tried to remove setEncoding, but no luck so far.

The code for In-Reply-To:

$message->getHeaders()->addHeaderLine('In-Reply-To', $in_re_to);

If I \Log::info($in_re_to) everything looks fine. I dont see these weird UTF-8? thing.

I am using Zend Mail V 2.3.

->addHeaderLine() handles value as a generic header. I think that is where wrong encoding happens.
What you should try instead is to add header object directly:

$messageid = new Header\MessageId();
$messageid->setId(null); // calls createMessageId() internally

$message->getHeaders()->addHeader($messageId);