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