/*** Encodes the given string for proper display in UTF-8.** This version is using base64 encoded data. The downside of this* is if the mail client does not understand this encoding the user* is basically doomed with an unreadable subject.** Please note that this version fully supports RFC 2045 section 6.8.*/function mail_encode($str){ // define start delimimter, end delimiter and spacer $start = "=?UTF-8?B?"; $end = "?="; $spacer = $end . ' ' . $start; $split_length = 64; $encoded_str = base64_encode($str); // If encoded string meets the limits, we just return with the correct data. if (strlen($encoded_str) <= $split_length) { return $start . $encoded_str . $end; } // If there is only ASCII data, we just return what we want, correctly splitting the lines. if (strlen($str) === utf8_strlen($str)) { return $start . implode($spacer, str_split($encoded_str, $split_length)) . $end; } // UTF-8 data, compose encoded lines $array = utf8_str_split($str); $str = ''; while (sizeof($array)) { $text = ''; while (sizeof($array) && strlen(base64_encode($text . $array[0])) <= $split_length) { $text .= array_shift($array); } $str .= $start . base64_encode($text) . $end . ' '; } return substr($str, 0, -1);}