7

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)
}
R_User
  • 10,682
  • 25
  • 79
  • 120
  • 2
    Could you paste the result of `var_dump($overview)`? – Florent Aug 16 '12 at 14:54
  • 1
    This is just plain weird and decidedly unexpected behaviour, your code does not really appear to be at fault here. The only thing I can suggest is that it may not like the missing space between the `:` and the header data when you set the `From:` header. Try changing `$header = "From:".mb_encode_mimeheader(...` to `$header = "From: ".mb_encode_mimeheader(...` and see if that makes a difference - although I sort of doubt it will. – DaveRandom Aug 16 '12 at 16:26
  • 1
    It seems that `imap_fetch_overview()` and some mail-Programms cant handle the brackets [] : As I said: when I remove the brackets, everythings works fine. I looked into RFC2822 http://www.faqs.org/rfcs/rfc2822.html (3.2.1. Primitive Tokens), and thought that I also have to escapce/encode the brackets. I checked several mailsheaders, and saw, that brackets are often used for IP-adresses. But I dont know the exact meaning of [] in mailheaders. – R_User Aug 16 '12 at 16:32
  • And `From: "Name[with brackets]" ` does not work? – dualed Aug 16 '12 at 17:11
  • 1
    When using quotes, PHP does not show the warning. But still I don't know if this is a PHP problem, or quotes need to be set in this case due to the RFC. I started some other thread approching the problem from different sides, and I always got the answer "don't use quotes in your header, it is no neccessary". That made me think, this might be a problem of PHP, and not of an incorrect header. It would be great to have a function, that encodes/escapes all characters that have a special meaning in the header, to not have problem when using them in the "alternative name"-part of the header. – R_User Aug 20 '12 at 13:25
  • I'd be looking at your test message in another IMAP client to help see if the encoding is wrong or the parsing is wrong. I say this because it might be a bug in the c-client library that lies underneath the `imap_*` functions. This library is known to be quirky and even buggy - and some bugs have never been fixed. – staticsan Aug 30 '12 at 00:24
  • The awarded answer does not make any sense since you are getting the address filed in headers. It is not OK to do that for an answer that doesn't have anything to do with the problem. – Mihai Iorga Aug 31 '12 at 10:34

3 Answers3

3

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
JochenJung
  • 7,183
  • 12
  • 64
  • 113
3

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

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
1

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().

John Garreth
  • 1,112
  • 2
  • 10
  • 17