imap php亂碼問題的解決辦法:首先開啓相應的PHP檔案;然後新增iconv進行字元編碼格式轉換即可解決亂碼問題,其語句如「iconv('gb2312','utf8',imap_base64($text));」。
推薦:《》
php imap/pop3 接收郵件類,解決中文亂碼
小弟目的想在嵌入式開發板上實現接收郵件,當然谷歌了一下,發現還是有很多實現方法的:php最簡單,C socket實現效率最高(我是這麼感覺),當然也少不了python實現(不過還沒動手測試)等等。
今天先介紹一下php 接受郵件類,這個類最初是老外Mitul Koradia寫的,感覺實現功能也很完整:
該類的主要方法如下:
class receiveMail { ... function receiveMail($username,$password,$EmailAddress,$mailserver='localhost',$servertype='pop',$port='110',$ssl = false) //Constructure function getHeaders($mid) // Get Header info function getTotalMails() //Get Total Number off Unread Email In Mailbox function getUnreadMails() //Get Number of Unread Mail from Mailbox function searchUnreadMails() //Find Numbers of Unread Mail by imap_search Method function GetAttach($mid,$path) // Get Atteced File from Mail function getBody($mid) // Get Message Body function deleteMails($mid) // Delete That Mail function close_mailbox() //Close Mail Box
不過有些瑕疵,我閱讀後稍作修改,主要是郵件頭沒進行MIME解碼,郵件中文內容亂碼。
首先關於郵件頭的解碼過程,Mitul Koradia的處理方法如下:
function getHeaders($mid) // Get Header info { if(!$this->marubox) return false; $mail_header=imap_header($this->marubox,$mid); $sender=$mail_header->from[0]; $sender_replyto=$mail_header->reply_to[0]; if(strtolower($sender->mailbox)!='mailer-daemon' && strtolower($sender->mailbox)!='postmaster') { $mail_details=array( 'from'=>strtolower($sender->mailbox).'@'.$sender->host, 'fromName'=>$sender->personal, 'subject'=>$mail_header->subject, 'to'=>$mail_header->toaddress ); } return $mail_details; }
但是返回結果是MIME編碼字元,顯然不行,於是新增了imap_mime_header_decode方法後就可以了。
function getHeaders($mid) // Get Header info { if(!$this->marubox) return false; $mail_header=imap_header($this->marubox,$mid); $sender=$mail_header->from[0]; $sender_replyto=$mail_header->reply_to[0]; if(strtolower($sender->mailbox)!='mailer-daemon' && strtolower($sender->mailbox)!='postmaster') { $mail_details=array( 'from'=>strtolower($sender->mailbox).'@'.$sender->host, 'fromName'=>imap_mime_header_decode($sender->personal)[0]->text, 'subject'=>imap_mime_header_decode($mail_header->subject)[0]->text, 'to'=>imap_mime_header_decode($mail_header->toaddress)[0]->text ); } return $mail_details; }
最後關於郵件中文內容亂碼的問題,老外肯定不會去考慮啦,其實也是比較簡單,新增iconv進行字元編碼格式轉換即可:
function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) //Get Part Of Message Internal Private Use { if(!$structure) { $structure = imap_fetchstructure($stream, $msg_number); } if($structure) { if($mime_type == $this->get_mime_type($structure)) { if(!$part_number) { $part_number = "1"; } $text = imap_fetchbody($stream, $msg_number, $part_number); if($structure->encoding == 3) { return iconv('gb2312','utf8',imap_base64($text)); } else if($structure->encoding == 4) { return iconv('gb2312','utf8',imap_qprint($text)); } else { return iconv('gb2312','utf8',$text); } } if($structure->type == 1) /* multipart */ { while(list($index, $sub_structure) = each($structure->parts)) { if($part_number) { $prefix = $part_number . '.'; } $data = $this->get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1)); if($data) { return $data; } } } } return false; }
初學PHP,感覺很多還不懂,希望能再接再厲
以上就是怎麼解決imap php 亂碼問題的詳細內容,更多請關注php中文網其它相關文章!