我正在使用以下代码通过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没有任何值(value)。

当“发件人:”部分不包含方括号时,就没有问题。 我还必须对方括号进行编码吗? 怎么样?我以为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)
  ["answered"]=>
  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=?=

这不是您想要的。

我找到了this function on 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

关于php - 读取带括号的 header 时,PHP imap_fetch_overview()函数是否存在错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11989915/

10-09 23:17
查看更多