Is there an error in PHP's imap_fetch_overview()-function when reading headers with brackets?

主宰稳场 提交于 2019-12-01 16:28:38

The problem ist, that

echo mb_encode_mimeheader("Name with [], ÄÖÜ and spaces");

returns

Name with [], =?UTF-8?B?w4PChMODwpbDg8KcIGFuZCBzcGFjZXM=?=

which is not, what you want.

I found this function on php.net, which might help you:

function EncodeMime($Text, $Delimiter) { 
    $Text = utf8_decode($Text); 
    $Len  = strlen($Text); 
    $Out  = ""; 
    for ($i=0; $i<$Len; $i++) 
    { 
        $Chr = substr($Text, $i, 1); 
        $Asc = ord($Chr); 

        if ($Asc > 0x255) // Unicode not allowed 
        { 
            $Out .= "?"; 
        } 
        else if ($Chr == " " || $Chr == $Delimiter || $Asc > 127) 
        { 
            $Out .= $Delimiter . strtoupper(bin2hex($Chr)); 
        } 
        else $Out .= $Chr; 
    } 
    return $Out; 
} 
echo EncodeMime("Name with [], ÄÖÜ and spaces", '%');

It returns

Name%20with%20[],%20%C4%D6%DC%20and%20spaces

The FROM field it's not added because you don't have from="john@doe.com" active (check ;) in php.ini and your mb_encode_mimeheader it's not converting as you want and needed for an email to dispatch correctly.

For an email with UTF-8 characters and name you should encode with UTF-8 and base64_encode.

Here's a proper way to sent that email:

$recipient  = "mail@server.tld";
$subject    = "=?UTF-8?B?".base64_encode('Subject äöü')."?=";
$text       = "Hallo";
$from_user  = "=?UTF-8?B?".base64_encode('Name with [], ÄÖÜ and spaces')."?= <webmaster@example.com>";
$header     = "From: ".$from_user." "
                . "\r\n" . "Reply-To: ".$from_user." "
                . "\r\n" . "MIME-Version: 1.0"
                . "\r\n" . "Content-Transfer-Encoding: 8bit"
                . "\r\n" . "Content-type: text/plain; charset=UTF-8"
                . "\r\n" . "X-Mailer: PHP/" . phpversion();

// send e-mail
mail($recipient, $subject, $text, $header);

You should receive the email with proper Name, Email and Subject. You will still need to decode those with iconv_mime_decode($overview->from,0, "UTF-8")

I had the same problem with brackets a while ago. Nobody could tell me, what the meaning of brackets in mail-header-adress-fields is and why PHP cant manage them. My solution was to simply remove brackets from the address-field after using imap_fetchheader().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!