php常用mime,mime.php

这篇博客讨论了MIME在电子邮件头文件中的作用,以及如何通过修改和优化PHP函数来改进MIME邮件的显示和解码。文章详细介绍了对`mime.php`文件的改动,包括检查和解码MIME邮件内容、调整邮件头的解码方式以及自定义的`utf8convert`函数,以处理乱码问题。此外,还提到了针对不同字符集的转换策略和邮件头长度的调整,以确保在不同浏览器下的兼容性。
摘要由CSDN通过智能技术生成

MIME是通過標準化電子郵件報文的頭部的附加域(fields)而實現的;這些頭部的附加域,描述新的報文類型的內容和組織形式。mime.php 包含檢查及解碼 MIME 郵件所需的函式。

[root@mail squirrelmail-1.4.8]# diff -uwBN functions/mime.php /var/www/squirrelmail/functions/mime.php

--- functions/mime.php2006-06-20 14:14:53.000000000 +0800

+++ /var/www/squirrelmail/functions/mime.php2014-01-10 09:35:57.104824062 +0800

修改郵件內容顯示方式。

@@ -288,7 +288,7 @@

$body_ary[$i] = $line;

}

- $body = '

' . implode("\n", $body_ary) . '
';

+ $body = implode("
\n", $body_ary);// . ''; //dywang

}

/**

原檔名常出現亂碼,改成先經過自行撰寫的函式 utf8convert 解碼後不再出現亂碼。

@@ -465,7 +465,7 @@

}

}

- $display_filename = $filename;

+ $display_filename = utf8convert($filename,''); //dywang

if (isset($passed_ent_id)) {

$passed_ent_id_link = '&passed_ent_id='.$passed_ent_id;

} else {

修改郵件 header 解碼方式。

@@ -495,7 +499,7 @@

$defaultlink = $hookresults[6];

$attachments .= '

' .

- ''.decodeHeader($display_filename).' 

' .

+ ''.decodeHeader($display_filename,false,false,true).' ' .//dywang

'

' . show_readable_size($header->size) .

'  

' .

'

[ '.htmlspecialchars($type0).'/'.htmlspecialchars($type1).' ] ' .

decodeHeader 函式主導郵件編碼的解碼,$utfencode=true 表示郵件為 utf8 編碼,$htmlsave=true 表示保留 html 格式,$decide=false 表示決定不必解碼。如果 Header 出現 =?,而且沒出現 =?,也就是出現編碼開始,但沒有編碼結束,就刪除換行等符號,直到變數 $pre_chunk 包含整個郵件的 header,以利解碼。

-function decodeHeader ($string, $utfencode=true,$htmlsave=true,$decide=false) {

+function decodeHeader ($string, $utfencode=true,$htmlsave=true,$decide=false) { //dywang debug this function

global $languages, $squirrelmail_language,$default_charset;

if (is_array($string)) {

$string = implode("\n", $string);

@@ -604,8 +615,10 @@

$iLastMatch = -2;

$encoded = false;

+static $pre_chunk; //dywang

$aString = explode(' ',$string);

$ret = '';

foreach ($aString as $chunk) {

if ($encoded && $chunk === '') {

continue;

@@ -616,8 +629,14 @@

$encoded = false;

/* if encoded words are not separated by a linear-space-white we still catch them */

$j = $i-1;

-

+if (preg_match('/^.*=\?[^?]*/',$chunk) && !preg_match('/[^?]*\?=.*$/',$chunk)) { //dywang begin

+$pre_chunk = preg_replace("/[\n\t\r]/", '',$chunk);

+$chunk='';

+} else{// if (!preg_match(preg_match('/^.*=\?/',$pre_chunk) && preg_match('/\?=.*$/',$chunk)) {

+$chunk = $pre_chunk . $chunk;

+$pre_chunk = '';

+} // dywang end

/* if the last chunk isn't an encoded string then put back the space, otherwise don't */

if ($iLastMatch !== $j) {

if ($htmlsave) {

因已自行設計解碼函式,故不再先測試是否可安全轉碼。

@@ -634,9 +653,12 @@

$ret .= $res[1];

}

$encoding = ucfirst($res[3]);

/* decide about valid decoding */

- if ($decide && is_conversion_safe($res[2])) {

+ if ($decide) {

$can_be_encoded=true;

} else {

$can_be_encoded=false;

如果必須解碼,改採用自行設計解碼函式 utf8convert。

@@ -648,10 +670,12 @@

$replace = base64_decode($res[4]);

if ($can_be_encoded) {

// string is converted from one charset to another. sanitizing depends on $htmlsave

- $replace = charset_convert($res[2],$replace,$default_charset,$htmlsave);

+$replace = utf8convert($replace,$res[2]); //dywang

} elseif ($utfencode) {

// string is converted to htmlentities and sanitized

$replace = charset_decode($res[2],$replace);

@@ -664,7 +688,8 @@

$replace);

if ($can_be_encoded) {

// string is converted from one charset to another. sanitizing depends on $htmlsave

- $replace = charset_convert($res[2], $replace,$default_charset,$htmlsave);

+$replace = utf8convert($replace,$res[2]); //dywang

} elseif ($utfencode) {

// string is converted to html entities and sanitized

$replace = charset_decode($res[2], $replace);

改變編碼偵測順序。

@@ -723,7 +749,7 @@

}

// Use B encoding for multibyte charsets

- $mb_charsets = array('utf-8','big5','gb2313','euc-kr');

+ $mb_charsets = array('utf-8','gb2313','big-5','euc-cn','cp936');

if (in_array($default_charset,$mb_charsets) &&

in_array($default_charset,sq_mb_list_encodings()) &&

sq_is8bit($string)) {

encodeHeader 函式郵件抬頭長度 75 不適合中文,改成 120 比較適合。

@@ -735,9 +761,10 @@

// Encode only if the string contains 8-bit characters or =?

$j = strlen($string);

- $max_l = 75 - strlen($default_charset) - 7;

+ $max_l = 120 - strlen($default_charset) - 7; //dywang 75 亂碼 for full_name

$aRet = array();

$ret = '';

$iEncStart = $enc_init = false;

@@ -897,7 +924,7 @@

* remove =? ? ?= (5 chars)

* remove 2 more chars (\r\n ?)

*/

- $iMaxLength = 75 - strlen($charset) - 7;

+ $iMaxLength = 120 - strlen($charset) - 7; //dywang

// set first character position

$iStartCharNum = 0;

如果使用 IE 瀏覽器,取消檔名轉換成 RFC1738 編碼。

@@ -2152,7 +2326,7 @@

// version

//set all the Cache Control Headers for IE

if ($isIE) {

- $filename=rawurlencode($filename);

+ //$filename=rawurlencode($filename); //dywang

header ("Pragma: public");

header ("Cache-Control: no-store, max-age=0, no-cache, must-revalidate"); // HTTP/1.1

header ("Cache-Control: post-check=0, pre-check=0", false);

自行建立的解碼函式,目前已是最佳狀況,出現亂碼的機率很低。

+/**

+ * This is a function to convert string coding to UTF-8.

+ *

+ * Author De-Yu Wang 2012/04/28 dywang

+ * @param $str the string

+ * @param $default_charset

+ * @return a string encoded by UTF-8

+ */

+function utf8convert($str, $actural) { //dywang optimal, Don't change

+global $default_charset;

+if(stristr($actural,'UTF-8')) return $str;//.'!&'.$actural;

+if(stristr($actural,'gb2312') || stristr($actural,'gb18030'))

+return mb_convert_encoding($str,$default_charset,'GBK');//.'!#'.$actural;

+if($actural!="" && !stristr($actural,'gb18030'))

+return mb_convert_encoding($str,$default_charset,$actural);//.'!+'.$actural;//.$default_charset.$detect;

+$encoding_list = 'UTF-8,GB2312,BIG-5,CP936, GBK, EUC-CN';

+$detect = mb_detect_encoding($str,$encoding_list);

+if(stristr($detect,'UTF'))

+return $str;//.'!/'. $detect;

+if(stristr($detect,'GB') || stristr($detect,'!EUC-CN'))

+return mb_convert_encoding($str,$default_charset,'GB2312');//.'!-'.$detect;

+if(stristr($detect,'C' ) || stristr($detect,'BIG'))

+return mb_convert_encoding($str,$default_charset,$detect);//.'!x'.$detect;

+if($detect == '')

+return mb_convert_encoding($str,$default_charset,'EUC-CN');//.'!C'.$detect;

+return $str;//.'!/'. $detect;

+}

+

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值