R_U*_*ser 7 php email email-headers
我正在使用以下代码在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);
?>
Run Code Online (Sandbox Code Playgroud)
之后我尝试使用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>";
?>
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Notice: Undefined property: stdClass::$from in /mail_test.php on line 34
Run Code Online (Sandbox Code Playgroud)
而$overview->from没有价值.
当"From:" - part不包含括号时,没有问题.我是否还必须对括号进行编码?怎么样?我以为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)
}
Run Code Online (Sandbox Code Playgroud)
问题是,
\n\necho mb_encode_mimeheader("Name with [], \xc3\x84\xc3\x96\xc3\x9c and spaces");\nRun Code Online (Sandbox Code Playgroud)\n\n回报
\n\nName with [], =?UTF-8?B?w4PChMODwpbDg8KcIGFuZCBzcGFjZXM=?=\nRun Code Online (Sandbox Code Playgroud)\n\n这不是你想要的。
\n\n我在 php.net 上找到了这个函数,它可能对你有帮助:
\n\nfunction EncodeMime($Text, $Delimiter) { \n $Text = utf8_decode($Text); \n $Len = strlen($Text); \n $Out = ""; \n for ($i=0; $i<$Len; $i++) \n { \n $Chr = substr($Text, $i, 1); \n $Asc = ord($Chr); \n\n if ($Asc > 0x255) // Unicode not allowed \n { \n $Out .= "?"; \n } \n else if ($Chr == " " || $Chr == $Delimiter || $Asc > 127) \n { \n $Out .= $Delimiter . strtoupper(bin2hex($Chr)); \n } \n else $Out .= $Chr; \n } \n return $Out; \n} \necho EncodeMime("Name with [], \xc3\x84\xc3\x96\xc3\x9c and spaces", \'%\');\nRun Code Online (Sandbox Code Playgroud)\n\n它返回
\n\nName%20with%20[],%20%C4%D6%DC%20and%20spaces\nRun Code Online (Sandbox Code Playgroud)\n