读取带括号的标头时,PHP 的 imap_fetch_overview() 函数是否有错误


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

我使用以下代码在PHP中发送电子邮件:

<?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);
?>

之后,我尝试使用以下代码中的imap_fetch_overview()阅读电子邮件:

<?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>";
?>

我收到以下错误:

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

$overview->from没有价值。

当"发件人:"部分不包含括号时,没有问题。我还必须对括号进行编码吗?如何?我以为mb_encode_mimeheader()正在做这项工作。

编辑:

var_dump($overview)的结果是:

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)
  ["answerswered"]=>
  int(0)
  ["deleted"]=>
  int(0)
  ["seen"]=>
  int(0)
  ["draft"]=>
  int(0)
  ["udate"]=>
  int(1345129104)
}

问题是,那

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

返回

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

这不是你想要的。

我在 php.net 上找到了这个功能,它可能会帮助你:

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", '%');

它返回

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

FROM 字段 它没有添加,因为您在php.ini和您的mb_encode_mimeheader中没有from="john@doe.com"活动(选中;)它没有按照您想要的方式转换,并且需要电子邮件才能正确发送。

对于带有 UTF-8 字符和名称的电子邮件,您应该使用 UTF-8base64_encode 进行编码。

以下是发送该电子邮件的正确方法:

$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);

您应该会收到包含正确姓名,电子邮件和主题的电子邮件。您仍然需要使用iconv_mime_decode($overview->from,0, "UTF-8")解码它们

不久前我在括号上遇到了同样的问题。没有人能告诉我,邮件头地址字段中括号的含义是什么,以及为什么PHP不能管理它们。我的解决方案是在使用imap_fetchheader()后简单地从地址字段中删除括号。