怎么解决imap php 乱码问题

  • 来源:网络
  • 更新日期:2020-08-12

摘要:imap php乱码问题的解决办法:首先打开相应的PHP文件;然后添加iconv进行字符编码格式转换即可解决乱码问题,其语句如“iconv('gb2312','utf8',imap_base64($tex

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,感觉很多还不懂,希望能再接再厉