Home  >  Article  >  Backend Development  >  How to solve imap php garbled problem

How to solve imap php garbled problem

藏色散人
藏色散人Original
2020-08-12 10:32:173041browse

The solution to the garbled problem of imap php: first open the corresponding PHP file; then add iconv to convert the character encoding format to solve the garbled problem. The statement is such as "iconv('gb2312','utf8',imap_base64 ($text));".

How to solve imap php garbled problem

Recommendation: "PHP Video Tutorial"

php imap/pop3 receive mail class, solve Chinese Garbled code

My purpose is to receive emails on an embedded development board. Of course, after searching on Google, I found that there are many implementation methods: PHP is the simplest, and C socket is the most efficient (this is how I feel) ), of course the python implementation is also indispensable (but I haven’t tested it yet) and so on.

Today I will first introduce the PHP mail-receiving class. This class was originally written by foreigner Mitul Koradia. I feel that the implementation function is also very complete:

The main methods of this class are as follows:

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

However, there are some flaws. I modified it slightly after reading it. The main reason is that the email header was not MIME decoded and the Chinese content of the email was garbled.

First of all, regarding the decoding process of the email header, Mitul Koradia's processing method is as follows:

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;
}

But the return result is MIME encoded characters, which obviously doesn't work, so I added the imap_mime_header_decode method and it worked.

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;
}

Finally, foreigners will definitely not consider the problem of garbled Chinese content in emails. In fact, it is relatively simple. Just add iconv to convert the character encoding format:

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; 
}

I feel like I am new to PHP. I still don’t understand many of them, I hope I can continue to work hard

The above is the detailed content of How to solve imap php garbled problem. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn