[problems encountered]
try to use the imap_fetchstructure function of PHP to get the encoding, of the message content transfer encoding for the corresponding decoding operation. However, after testing, it is found that the representative value of encoding returned is not always consistent with the actual coding, such as--
Base64 encoding 307bit
this email actually uses Quoted-Printable encoding. The expected function encoding return value should be 4, but the actual return value is 0 (7bit).
[related Code]
function retrieve_message($auth_user,$accountid,$messageid,$fullheaders){
$message=array();
if(!($auth_user && $accountid && $messageid)){
return false;
}
$imap=open_mailbox($auth_user,$accountid);
if(!$imap){
return false;
}
/*
header
*/
$structure=imap_fetchstructure($imap,$messageid);
$encoding=$structure->encoding;
$message["body"]=imap_body($imap,$messageid);
if(!$message["body"]){
$message["body"]="[This message has no body.]";
}
if(($encoding==3) || stristr($message["body"],"Content-Transfer-Encoding: base64")){
$message["body"]=base64_decode($message["body"]);
if(is_gbk($message["body"])){
$message["body"]=iconv("gbk","utf-8",$message["body"]);
}
} elseif(($encoding==4) || stristr($message["body"],"Content-Transfer-Encoding: quoted-printable")){
$message["body"]=quoted_printable_decode($message["body"]);
if(is_gbk($message["body"])){
$message["body"]=iconv("gbk","utf-8",$message["body"]);
}
}
imap_close($imap);
return $message;
}
[attempts made]
searched the Internet for a long time, but could not find a particularly clear answer. I only saw that some articles mentioned that there could be multiple parts, for a complex email and the main content was generally in parts [1], so I added the following judgment--
if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1])){
$encoding=$structure->parts[1]->encoding;
} else {
$encoding=$structure->encoding;
}
but only a small number of original problem emails get the correct encoding, and most of them don"t work.
[Test Environment]
XAMPP
operating system: Windows 10 Family Chinese version 1803
Apache version: 2.4.16 (Win32)
PHP version: 5.6.12