Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm sending an E-Mail in PHP using the following code:

<?php
error_reporting(E_ALL);

# write mail
###############################################################################
$recipient  = "[email protected]";
$subject    = mb_encode_mimeheader("Subject äöü ");
$text       = "Hallo";
$header     = "From:".mb_encode_mimeheader("Name with [], ÄÖÜ and spaces")." <[email protected]>"
                . "\r\n" . "Reply-To: [email protected]"
                . "\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}", "[email protected]", "********");

$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) "[email protected]"
  ["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)
}
like image 681
R_User Avatar asked Aug 16 '12 14:08

R_User


2 Answers

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
like image 117
JochenJung Avatar answered Oct 09 '22 01:10

JochenJung


The FROM field it's not added because you don't have from="[email protected]" 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  = "[email protected]";
$subject    = "=?UTF-8?B?".base64_encode('Subject äöü')."?=";
$text       = "Hallo";
$from_user  = "=?UTF-8?B?".base64_encode('Name with [], ÄÖÜ and spaces')."?= <[email protected]>";
$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")

like image 40
Mihai Iorga Avatar answered Oct 09 '22 03:10

Mihai Iorga