public function encodeHeader($str, $position = 'text') {
        $encoded = core_text::encode_mimeheader($str, $this->CharSet);
        if ($encoded !== false) {
            if ($position === 'phrase') {
                // Escape special symbols in each line in the encoded string, join back together and enclose in quotes.
                $chunks = preg_split("/\\n/", $encoded);
                $chunks = array_map(function($chunk) {
                    return addcslashes($chunk, "\0..\37\177\\\"");
                }, $chunks);
                return '"' . join(parent::getLE(), $chunks) . '"';
            }
            return str_replace("\n", parent::getLE(), $encoded);
        }

        return parent::encodeHeader($str, $position);
    }