在使用statusNet做微博客的时候,发送中文邀请时,在一些收信软件上,或者在线的邮箱里,信件的主题一项会显示成乱码。具体成因不详。
为了解决这个问题,又做google research,找到解决方案,并对statusNet原代码做了一点点修改,发现中文邮件题头乱码问题得到解决。效果如下:图中第一封信为修改代码后的显示效果,第二封信为修改代码前的显示效果。
打开lib/mail.php文件,对原代码段:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function mail_send($recipients, $headers, $body) { // XXX: use Mail_Queue... maybe $backend = mail_backend(); if (!isset($headers['Content-Type'])) { $headers['Content-Type'] = 'text/plain; charset=UTF-8'; } assert($backend); // throws an error if it's bad $sent = $backend->send($recipients, $headers, $body); if (PEAR::isError($sent)) { common_log(LOG_ERR, 'Email error: ' . $sent->getMessage()); return false; } return true; } |
修改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | function utf8_encode_c($string) { $charset = 'utf-8'; $encoded_string = $string; if(strtolower($charset) == 'utf-8' && preg_match('/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff]/', $string)) { // Define start delimimter, end delimiter and spacer $end = "?="; $start = "=?" . $charset . "?B?"; $spacer = $end . ' ' . $start; // Determine length of encoded text within chunks and ensure length is even (should NOT use the my_strlen functions) $length = 75 - strlen($start) - strlen($end); $length = floor($length/4) * 4; // Encode the string and split it into chunks with spacers after each chunk $encoded_string = base64_encode($encoded_string); $encoded_string = chunk_split($encoded_string, $length, $spacer); // Remove trailing spacer and add start and end delimiters $spacer = preg_quote($spacer); $encoded_string = preg_replace("/" . $spacer . "$/", "", $encoded_string); $encoded_string = $start . $encoded_string . $end; } return $encoded_string; } function mail_send($recipients, $headers, $body) { // XXX: use Mail_Queue... maybe $backend = mail_backend(); if (!isset($headers['Content-Type'])) { $headers['Content-Type'] = 'text/plain; charset=UTF-8'; $headers['Subject'] = utf8_encode_c(cleanup_mail($headers['Subject'])); } assert($backend); // throws an error if it's bad $sent = $backend->send($recipients, $headers, $body); if (PEAR::isError($sent)) { common_log(LOG_ERR, 'Email error: ' . $sent->getMessage()); return false; } return true; } |
转载请注明文章来自糗世界博客


你真是个技术男…
[回复]
admin 回复:
二月 8th, 2010 at 4:57 下午
@Changan,过奖了。
[回复]