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?<>?=
Message-ID: =?UTF-8?Q?<>?=

How it should look:

In-Reply-To: <>
Message-ID: <>

The code I use to create the message-ID:

$messageid = new Header\MessageId();


$_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