public static function encodeValue($value, $wrapWidth = 998, $folding = "\r\n ")
    {
        if (!preg_match('~[^\x00-\x7F]~', $value)) {
            $encoding = '7bit';
        } elseif (preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $value) > (strlen($value) / 3)) {
            $encoding = 'base64';
        } else {
            $encoding = 'quoted-printable';
        }

        $encodedName = Mime::encode($value, $encoding, $wrapWidth, $folding, true);

        $value = trim(str_replace(["\r", "\n"], '', $value));

        if ($encoding === 'base64') {
            $encodedName = "=?UTF-8?B?{$encodedName}?=";
        } elseif ($encoding === 'quoted-printable') {
            $encodedName = "=?UTF-8?Q?{$encodedName}?=";
        } else {
            $encodedName = addcslashes($encodedName, "\0..\37\177\\\"");
            if ($encodedName !== $value || preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $value) === 1) {
                $encodedName = sprintf('"%s"', $encodedName);
            }
        }
        return $encodedName;
    }