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

倾然丶 夕夏残阳落幕 提交于 2019-12-01 15:27:00

问题


I'm sending an E-Mail in PHP using the following code:

<?php
error_reporting(E_ALL);

# write mail
###############################################################################
$recipient  = "mail@server.tld";
$subject    = mb_encode_mimeheader("Subject äöü ");
$text       = "Hallo";
$header     = "From:".mb_encode_mimeheader("Name with [], ÄÖÜ and spaces")." <webmaster@example.com>"
                . "\r\n" . "Reply-To: webmaster@example.com"
                . "\r\n" . "X-Mailer: PHP/" . phpversion();

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

Afterwards I try to read the e-Mail using imap_fetch_overview() in the following code:

<?php
# receive mails
###############################################################################
$mailbox        = imap_open("{imap.server.tld/norsh}", "mail@server.tld", "********");

$MC = imap_check($mailbox);
$result = imap_fetch_overview($mailbox,"1:{$MC->Nmsgs}",0);

echo "<table>";
foreach ($result as $overview) {
    echo "<tr>"
        ."<td>".$overview->msgno."</td>"
        ."<td>".$overview->uid."</td>"
        ."<td>".$overview->date."</td>"
        ."<td>".$overview->udate."</td>"
        ."<td>".$overview->from."</td>"
        ."<td>".$overview->to."</td>"
        ."<td>".$overview->size."</td>"
        ."<td>".$overview->subject."</td>"
        ."</tr>";
}
echo "</table>";
?>

I get the following error:

Notice: Undefined property: stdClass::$from in /mail_test.php on line 34

And $overview->from has no value.

When the "From: "-part does not contain brackets, there is no problem. Do I also have to encode the brackets? How? I thought mb_encode_mimeheader() is doing the job.

EDIT:

The result of var_dump($overview) is:

object(stdClass)#18 (14) {
  ["subject"]=>
  string(40) "Subject =?UTF-8?B?w4PCpMODwrzDg8K2IA==?="
  ["to"]=>
  string(16) "mail@server.tld"
  ["date"]=>
  string(31) "Thu, 16 Aug 2012 16:58:23 +0200"
  ["message_id"]=>
  string(58) "<**************************************>"
  ["size"]=>
  int(1585)
  ["uid"]=>
  int(18)
  ["msgno"]=>
  int(17)
  ["recent"]=>
  int(1)
  ["flagged"]=>
  int(0)
  ["answered"]=>
  int(0)
  ["deleted"]=>
  int(0)
  ["seen"]=>
  int(0)
  ["draft"]=>
  int(0)
  ["udate"]=>
  int(1345129104)
}

回答1:


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



回答2:


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")




回答3:


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().



来源:https://stackoverflow.com/questions/11989915/is-there-an-error-in-phps-imap-fetch-overview-function-when-reading-headers-w

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