用PHP實(shí)現(xiàn)POP3郵件的解碼(三)
實(shí)現(xiàn) MIME 解碼的類(lèi)
該類(lèi)實(shí)現(xiàn)解碼的方法是 decode($head=null,$body=null,$content_num=-1),為了處理上的方便,要求輸入的是兩個(gè)字符數(shù)組,在我們的上篇中,所用到的POP類(lèi)所收取得到的就是兩個(gè)這樣的數(shù)組,一個(gè)是郵件頭內(nèi)容,一個(gè)是郵件的正文內(nèi)容。限于篇幅,不對(duì)其做詳細(xì)的說(shuō)明,其實(shí)現(xiàn)思想跟本文上篇中所介紹的POP類(lèi)類(lèi)似。請(qǐng)參考其中的注釋。
該類(lèi)中用到了大量的正則表達(dá)式的操作,對(duì)此不熟悉的讀者,請(qǐng)參考正則表達(dá)式的有關(guān)資料。
class decode_mail{var $from_name;var $to_name;var $mail_time;var $from_mail;var $to_mail;var $reply_to;var $cc_to;var $subject;
// 解碼后的郵件頭部分的信息:var $body;
// 解碼后得到的正文數(shù)據(jù),為一個(gè)數(shù)組。var $body_type; // 正文類(lèi)型var $tem_num=0;var $get_content_num=0;var $body_temp=array();var $body_code_type;var $boundary;
// 以上是一些方法中用到的一些全局性的臨時(shí)變量,由于 PHP不能做到良好的封裝,所以只能放在這里定義var $err_str; // 錯(cuò)誤信息var $debug=0; // 調(diào)試標(biāo)記var $month_num=array('Jan'=>1,'Feb'=>2,'Mar'=>3,'Apr'=>4,'May'=>5,'Jun'=>6,'Jul'=>7,'Aug'=>8,'Sep'=>9,'Oct'=>10,'Nov'=>11,'Dec'=>12); // 把英文月份轉(zhuǎn)換成數(shù)字表示的月份
function decode($head=null,$body=null,$content_num=-1) // 調(diào)用的主方法,$head 與 $body 是兩個(gè)數(shù)組,$content_num 表示的是當(dāng)正文有多個(gè)部分的時(shí)候,只取出指定部分的內(nèi)容以提高效率,默認(rèn)為 -1 ,表示解碼全部?jī)?nèi)容,如果解碼成功,該 方法返回 true{ if (!$head and !$body) { $this->err_str='沒(méi)有指定郵件的頭與內(nèi)容!!'; return false; }if (gettype($head)=='array') { $have_decode=true; $this->decode_head($head); }if (gettype($body)=='array') { $this->get_content_num=$content_num; $this->body_temp=$body; $have_decode=true; $this->decode_body(); unset($this->body_temp); }if (!$have_decode) { $this->err_str='傳遞的參數(shù)不對(duì),用法:new decode_mail(head,body) 兩個(gè)參數(shù)都是數(shù)組'; return false; }}function decode_head($head) // 郵件頭內(nèi)容 的解碼,取出郵件頭中有意義的內(nèi)容{ $i=0; $this->from_name=$this->to_name=$this->mail_time=$this->from_mail=$this-> to_mail=$this->reply_to=$this->cc_to=$this->subject=''; $this->body_type=$Sthis->boundary=$this->body_code_type=''; while ($head[$i]) { if (strpos($head[$i],'=?')) $head[$i]=$this->decode_mime($head[$i]); //如果有編碼的內(nèi)容,則進(jìn)行解碼,解碼函數(shù)是上文所介紹的 decode_mime() $pos=strpos($head[$i],':'); $summ=substr($head[$i],0,$pos); $content=substr($head[$i],$pos+1); //將郵件頭信息的標(biāo)識(shí)與內(nèi)容分開(kāi) if ($this->debug) echo $summ.':----:'.$content.'<BR>'; switch (strtoupper($summ)) { case 'FROM': // 發(fā)件人地址及姓名(可能沒(méi)有姓名,只有地址信息) if ($left_tag_pos=strpos($content,'<')) { $mail_lenth=strrpos($content,'>')-$left_tag_pos-1; $this->from_name=substr($content,0,$left_tag_pos); $this->from_mail=substr($content,$left_tag_pos+1,$mail_lenth); if (trim($this->from_name)=='') $this->from_name=$this->from_mail; else if (ereg('['|']([^'']+)['|']',$this->from_name,$reg)) $this->from_name=$reg[1]; } else { $this->from_name=$content; $this->from_mail=$content; //沒(méi)有發(fā)件人的郵件地址 } break; case 'TO': //收件人地址及姓名(可能 沒(méi)有姓名) if ($left_tag_pos=strpos($content,'<')) { $mail_lenth=strrpos($content,'>')-$left_tag_pos-1; $this->to_name=substr($content,0,$left_tag_pos); $this->to_mail=substr($content,$left_tag_pos+1,$mail_lenth); if (trim($this->to_name)=='') $this->to_name=$this->to_mail; else if (ereg('['|']([^'']+)['|']',$this->to_name,$reg)) $this->to_name=$reg[1]; } else { $this->to_name=$content; $this->to_mail=$content; //沒(méi)有分開(kāi)收件人的郵件地址 } break;
case 'DATE' : //發(fā)送日期,為了處理方便,這里返回的是一個(gè) Unix 時(shí)間戳,可以用 date('Y-m-d',$this->mail_time) 來(lái)得到一般格式的日期
$content=trim($content); $day=strtok($content,' '); $day=substr($day,0,strlen($day)-1); $date=strtok(' '); $month=$this->month_num[strtok(' ')]; $year=strtok(' '); $time=strtok(' '); $time=split(':',$time); $this->mail_time=mktime($time[0],$time[1],$time[2],$month,$date,$year); break; case 'SUBJECT': //郵件主題 $this->subject=$content; break; case 'REPLY_TO': // 回復(fù)地址(可能沒(méi)有) if (ereg('<([^>]+)>',$content,$reg)) $this->reply_to=$reg[1]; else $this->reply_to=$content; break; case 'CONTENT-TYPE': // 整個(gè)郵件的 Content類(lèi)型, eregi('([^;]*);',$content,$reg); $this->body_type=trim($reg[1]); if (eregi('multipart',$content)) // 如果是 multipart 類(lèi)型,取得 分隔符 { while (!eregi('boundary='(.*)'',$head[$i],$reg) and $head[$i]) $i++; $this->boundary=$reg[1]; } else //對(duì)于一般的正文類(lèi)型,直接取得其編碼方法 { while (!eregi('charset=['|'](.*)['|']',$head[$i],$reg)) $i++; $this->body_char_set=$reg[1]; while (!eregi('Content-Transfer-Encoding:(.*)',$head[$i],$reg)) $i++; $this->body_code_type=trim($reg[1]); } break; case 'CC': //抄送到。。 if (ereg('<([^>]+)>',$content,$reg)) $this->cc_to=$reg[1]; else $this->cc_to=$content; default: break; } // end switch $i++; } // end while if (trim($this->reply_to)=='') //如果沒(méi)有指定回復(fù)地址,則回復(fù)地址為發(fā)送人地址 $this->reply_to=$this->from_mail;}// end function define
function decode_body() //正文的解碼,其中用到了不少郵件頭解碼所得來(lái)的信息{$i=0;if (!eregi('multipart',$this->body_type)) // 如果不是復(fù)合類(lèi)型,可以直接解碼 { $tem_body=implode($this->body_temp,'rn'); switch (strtolower($this->body_code_type)) // body_code_type ,正文的編碼方式,由郵件頭信息中取得 {case 'base64': $tem_body=base64_decode($tem_body); break;
case 'quoted-printable': $tem_body=quoted_printable_decode($tem_body); break; ; }
$this->tem_num=0; $this->body=array(); $this->body[$this->tem_num][content_id]=''; $this->body[$this->tem_num][type]=$this->body_type; switch (strtolower($this->body_type))
{ case 'text/html': $this->body[$this->tem_num][name]='超文本正文'; break; case 'text/plain': $this->body[$this->tem_num][name]='文本正文'; break; default: $this->body[$this->tem_num][name]='未知正文'; } $this->body[$this->tem_num][size]=strlen($tem_body); $this->body[$this->tem_num][content]=$tem_body; unset($tem_body); } else // 如果是復(fù)合類(lèi)型的 { $this->body=array(); $this->tem_num=0; $this->decode_mult($this->body_type,$this->boundary,0);//調(diào)用復(fù)合類(lèi)型的解碼方法 }}
function decode_mult($type,$boundary,$begin_row) // 該方法用遞歸的方法實(shí)現(xiàn) 復(fù)合類(lèi)型郵件正文的解碼,郵件源文件取自于 body_temp 數(shù)組,調(diào)用時(shí)給出該復(fù)合類(lèi)型的類(lèi)型、分隔符及 在 body_temp 數(shù)組中的開(kāi)始指針
{$i=$begin_row;$lines=count($this->body_temp);while ($i<$lines) // 這是一個(gè)部分的結(jié)束標(biāo)識(shí); { while (!eregi($boundary,$this->body_temp[$i]))//找到一個(gè)開(kāi)始標(biāo)識(shí) $i++; if (eregi($boundary.'--',$this->body_temp[$i])) { return $i; }
while (!eregi('Content-Type:([^;]*);',$this->body_temp[$i],$reg ) and $this->body_temp[$i]) $i++; $sub_type=trim($reg[1]); // 取得這一個(gè)部分的 類(lèi)型是milt or text .... if (eregi('multipart',$sub_type))// 該子部分又是有多個(gè)部分的; { while (!eregi('boundary='([^']*)'',$this->body_temp[$i],$reg) and $this->body_temp[$i]) $i++; $sub_boundary=$reg[1];// 子部分的分隔符; $i++; $last_row=$this->decode_mult($sub_type,$sub_boundary,$i); $i=$last_row; } else { $comm=''; while (trim($this->body_temp[$i])!='') { if (strpos($this->body_temp[$i],'=?')) $this->body_temp[$i]=$this->decode_mime($this->body_temp[$i]); if (eregi('Content-Transfer-Encoding:(.*)',$this->body_temp[$i],$reg)) $code_type=strtolower(trim($reg[1])); // 編碼方式 $comm.=$this->body_temp[$i].'rn'; $i++; } // comm 是編碼的說(shuō)明部分
if (eregi('name=[']([^']*)[']',$comm,$reg)) $name=$reg[1];
if (eregi('Content-Disposition:(.*);',$comm,$reg)) $disp=$reg[1];
if (eregi('charset=['|'](.*)['|']',$comm,$reg)) $char_set=$reg[1];
if (eregi('Content-ID:[ ]*<(.*)>',$comm,$reg)) // 圖片的標(biāo)識(shí)符。 $content_id=$reg[1];
$this->body[$this->tem_num][type]=$sub_type; $this->body[$this->tem_num][content_id]=$content_id; $this->body[$this->tem_num][char_set]=$char_set; if ($name) $this->body[$this->tem_num][name]=$name; else switch (strtolower($sub_type)) { case 'text/html': $this->body[$this->tem_num][name]='超文本正文'; break;
case 'text/plain': $this->body[$this->tem_num][name]='文本正文'; break;
default: $this->body[$this->tem_num][name]='未知正文'; } // 下一行開(kāi)始取回正文 if ($this->get_content_num==-1 or $this->get_content_num==$this->tem_num) // 判斷這個(gè)部分是否是需要的。-1 表示全部 { $content=''; while (!ereg($boundary,$this->body_temp[$i])) { //$content[]=$this->body_temp[$i]; $content.=$this->body_temp[$i].'rn'; $i++; }
//$content=implode('rn',$content); switch ($code_type) { case 'base64': $content=base64_decode($content); break;
case 'quoted-printable': $content=str_replace('n','rn',quoted_printable_decode($content)); break; }
$this->body[$this->tem_num][size]=strlen($content); $this->body[$this->tem_num][content]=$content; } else { while (!ereg($boundary,$this->body_temp[$i])) $i++; } $this->tem_num++; } // end else} // end while;} // end function define
function decode_mime($string) {//decode_mime 已在上文中給出,這里略過(guò)。}} // end class define
在這里要特別說(shuō)明一點(diǎn)的是html正文里所用圖片的解碼。發(fā)送html格式的正文時(shí),都會(huì)碰到圖片如何傳送的問(wèn)題。圖片在 html 文檔里是一個(gè)<img src='http://www.hdgsjgj.cn/bcjs/21103.html' >的標(biāo)簽,關(guān)鍵是這個(gè)源文件從何來(lái)的。很多郵件的處理方法是用一個(gè)絕對(duì)的 url 標(biāo)識(shí),就是在郵件的html正文里用<img src= http://www.ccidnet.com/image/22.gif >之類(lèi)的標(biāo)簽,這樣,在閱讀郵件時(shí),郵件閱讀器(通常是用內(nèi)嵌的瀏覽器)會(huì)自動(dòng)從網(wǎng)上下載圖片,但是如果郵件收下來(lái)之后,與 Internet 的連接斷了,圖片也就不能正常顯示。
所以更好的方法是把圖片放在郵件中一起發(fā)送出去。在 MIME 編碼里,描述圖片與正文的關(guān)系,除了上面所提到的multipart/related MIME頭信息之外,還用到了一個(gè) Content-ID: 的屬性來(lái)使圖片與 html 正文之間建立關(guān)系。html 文檔中的圖片在編碼時(shí),其MIME頭中加入一個(gè) Content-ID:122223443556dsdf@ntsever 之類(lèi)的屬性,122223443556dsdf@ntsever是一個(gè)唯一的標(biāo)識(shí),在 html 文檔里,<img>標(biāo)簽被修改成<img src='cid: 122223443556dsdf@ntsever'>,在解碼的時(shí)候,實(shí)際上,還需要把 html 正文中的這些<img src>標(biāo)簽進(jìn)行修改,使之指向解碼后的圖片的具體路徑。但是考慮到具體的解碼程序中對(duì)圖片會(huì)有不同的處理,所以在這個(gè)解碼的類(lèi)中,沒(méi)有對(duì) hmtl 正文中的<img>標(biāo)簽進(jìn)行修改。所以在實(shí)際使用這個(gè)類(lèi)時(shí),對(duì)于有圖片的 html 正文,還需要一定的處理。正文中的圖片,可以用臨時(shí)文件來(lái)保存,也可以用數(shù)據(jù)庫(kù)來(lái)保存。
現(xiàn)在我們已經(jīng)介紹了POP3 收取郵件并進(jìn)行 MIME 解碼的原理。下面給出一個(gè)使用這兩個(gè)類(lèi)的一段小程序:
<?include('pop3.inc.php');include('mime.inc.php');$host='pop.china.com';$user='boss_ch';$pass='mypassword';$rec=new pop3($host,110,2);$decoder=new decode_mail();
if (!$rec->open()) die($rec->err_str);
if (!$rec->login($user,$pass)) die($rec->err_str);
if (!$rec->stat()) die($rec->err_str);echo '共有'.$rec->messages.'封信件,共'.$rec->size.'字節(jié)大小<br>';
if ($rec->messages>0) { if (!$rec->listmail()) die($rec->err_str); echo '以下是信件內(nèi)容:<br>'; for ($i=1;$i<=count($rec->mail_list);$i++) { echo '信件'.$rec->mail_list[$i][num].',大小:'.$rec->mail_list[$i][size].'<BR>'; $rec->getmail($rec->mail_list[$i][num]); $decoder->decode($rec->head,$rec->body); echo '<h3>郵件頭的內(nèi)容:</h3><br>'; echo $decoder->from_name.'('.$decoder->from_mail.') 于'.date('Y-m-d H:i:s',$decoder->mail_time).' 發(fā)給'.$decoder->to_name.'('.$decoder->to_mail.')'; echo 'n<br>抄送:';
if ($decoder->cc_to) echo $decoder->cc_to;else echo '無(wú)'; echo 'n<br>主題:'.$decoder->subject;
echo 'n<br>回復(fù)到:'.$decoder->reply_to; echo '<h3>郵件正文 :</h3><BR>'; echo '正文類(lèi)型:'.$decoder->body_type; echo '<br>正文各內(nèi)容:'; for ($j=0;$j<count($decoder->body);$j++) { echo 'n<br>類(lèi)型:'.$decoder->body[$j][type]; echo 'n<br>名稱(chēng):'.$decoder->body[$j][name]; echo 'n<br>大小:'.$decoder->body[$j][size]; echo 'n<br>content_id:'.$decoder->body[$j][content_id]; echo 'n<br>正文字符集'.$decoder->body[$j][char_set]; echo '<pre>'; echo '正文內(nèi)容:'.$decoder->body[$j][content]; echo '</pre>'; }$rec->dele($i); }}
$rec->close();?>
如有想要取得完整源代碼的朋友,請(qǐng)與本人聯(lián)系: boss_ch@netease.com
<全文完>
