private static function encodeHeader(string $s, int &$offset = 0, bool $quotes = false): string
    {
        if (\strspn($s,
                "!\"#$%&\'()*+,-./0123456789:;<>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^`abcdefghijklmnopqrstuvwxyz{|}~=? _\r\n\t") === \strlen($s)) {
            if ($quotes && \preg_match('#[^ a-zA-Z0-9!\#$%&\'*+/?^_`{|}~-]#', $s)) { // RFC 2822 atext except =
                return self::append('"' . \addcslashes($s, '"\\') . '"', $offset);
            }

            return self::append($s, $offset);
        }

        $o = '';
        if ($offset >= 55) { // maximum for iconv_mime_encode
            $o = self::EOL . "\t";
            $offset = 1;
        }

        $filed = \str_repeat(' ', $old = $offset);
        if (\function_exists('\iconv_mime_encode')) {
            $s = \iconv_mime_encode($filed, $s, [
                'scheme' => 'B', // Q is broken
                'input-charset' => 'UTF-8',
                'output-charset' => 'UTF-8',
            ]);
        } else {
            $s = $filed . ': ' . \mb_encode_mimeheader($s, 'UTF-8', 'B');
        }

        $offset = \strlen($s) - \strrpos($s, "\n");
        $s = \str_replace("\n ", "\n\t", \substr($s, $old + 2)); // adds ': '

        return $o . $s;
    }