您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

怎么解决imap php 乱码问题

2024/5/28 4:33:39发布12次查看
imap php乱码问题的解决办法:首先打开相应的php文件;然后添加iconv进行字符编码格式转换即可解决乱码问题,其语句如“iconv('gb2312','utf8',imap_base64($text));”。
推荐:《php视频教程》
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 乱码问题的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product