上一次提到这个问题的时候,似乎感觉问题解决了,但是后来发现,上次的代码并不能很好的解决问题,很多中英文混杂的标题无法正确的显示。

为此,不得不再次研究一下代码,将代码做了一下精减,反而效果还是不错的。具体代码如下:

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
function cleanup_mail($string)
    {
        $string = str_replace(array("\r", "\n", "\r\n"), "", $string);
        $string = trim($string);
        return $string;
    }
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?";
 
            // Encode the string
            $encoded_string = base64_encode($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;
}

转载请注明文章来自糗世界博客

Tags: , ,

One Response to “关于statusNet发中文邀请或者通知邮件时题头会产生乱码问题(续)”

  1. [...] 終於找到了三個資料,當然關於邀請函的亂碼問題,相信困擾很多使用Status架站的朋友,現在開始來動手將這個問題排除吧! 參考資訊:樂逍遙:StatusNet郵件中文亂碼 、糗世界:關於statusNet發中文邀請或者通知郵件時題頭會產生亂碼問題(續)、糗世界:關於statusNet發中文邀請或者通知郵件時題頭會產生亂碼問題 [...]

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">