7

我正在使用以下代码在 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没有价值。

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

3 回答 3

3

未添加 FROM 字段是因为您没有from="john@doe.com"活动(签;入),php.ini并且您mb_encode_mimeheader没有按照您的意愿进行转换,并且无法正确发送电子邮件。

对于带有 UTF-8 字符和名称的电子邮件,您应该使用UTF-8和进行编码base64_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")

于 2012-08-29T23:25:50.230 回答
3

问题在于,

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
于 2012-08-24T14:22:58.653 回答
1

不久前我在括号中遇到了同样的问题。没有人能告诉我,mail-header-adress-fields 中的括号是什么意思,为什么 PHP 不能管理它们。我的解决方案是在使用imap_fetchheader().

于 2012-08-31T10:22:41.783 回答