Welcome to Geeklog, Anonymous Friday, March 29 2024 @ 11:28 am EDT

Geeklog Forums

Support for UTF-8 e-mail headers


Status: offline

LWC

Forum User
Full Member
Registered: 02/19/04
Posts: 818
When not in English, e-mail headers can't be encoded in UTF-8!
I'm surprised there is currently no bug report about this. Probably because there's no problem with English UTF-8 e-mail headers.

Of course, most headers are technical, so obviously they would always be in English. This leaves us with just 3 headers, which also happen to be the most important ones...
1) "From".
2) "To".
3) "Subject".

Luckily, PHP 5 has brought us a great function called "iconv_mime_encode", which I've used to solve this problem. Now, I realize Geeklog insists on being PHP 4 friendly, so I've added a version check (PHP 4 users would miss out the fix).
Remember, without doing this, the aforementioned headers turn up in complete Gibberish!
Maybe you, as admins, won't care, but remember that, say, when a user registers to the site, they also get a message with a Gibberish subject and may delete the message because of it!

Anyway, I've submitted a patch for this.
 Quote

Status: offline

LWC

Forum User
Full Member
Registered: 02/19/04
Posts: 818
Thanks to a suggestion by a user called woop, I've modified my patch so PHP v4 users can enjoy UTF-8 in their e-mail too.
I'll maintain the patch here from now on (the official patch links to here).

I don't take responsibility on his code (all of the base64_encode) as I don't feel like testing outdated stuff Wink - but he claims it works like a charm.

Now, I need answers from everyone who wants this patch for a couple of questions:
  1. Right now because of woop, everything that isn't UTF-8 is converted to Base64. Is this a valid request by his part or should users like woop be forced to use UTF-8 in their sites and then I'll just do a general test for UTF-8 and only then activate any conversion?
  2. Same question for HTML mail. Geeklog didn't actually encode it and I fixed it but only for UTF-8 messages. Should I fix it for all encodings? If so, please provide me the proper code for it.
  3. What is the best way to deal with RTL? It can only be used in HTML messages anyway, and the only type of such messages used to be just admin messages and the admins can just use HTML and dir=rtl so I didn't enforce it even if the current language uses it. However, now you have plugins like Media Gallery that use HTML messages (in MG's case, for postcards) and it looks crippled without RTL.


public_html/lib-common.php (function COM_mail):
Text Formatted Code
...
    else
    {
        $charset = $LANG_CHARSET;
    }

// custom code © LWC - start
if (function_exists ('iconv_mime_encode') && $charset == "utf-8") {
$mime_parameters = array(
'input-charset' => 'utf-8',
'output-charset' => 'utf-8',
'scheme' => 'B'
);

if (strpos($from, " <") !== false)
if ($from != utf8_decode($from)) {
$from = split(" <", $from);
if (isset($mime_parameters))
$from[0] = substr(iconv_mime_encode('', $from[0], $mime_parameters), 2);
else
$from[0] = '=?' . $charset . '?B?' . base64_encode($from[0]) . '?= ';
$from = implode(" <", $from);
}

if (strpos($to, " <") !== false)
if ($to != utf8_decode($to)) {
$to = split(" <", $to);
if (isset($mime_parameters))
$to[0] = substr(iconv_mime_encode('', $to[0], $mime_parameters), 2);
else
$to[0] = '=?' . $charset . '?B?' . base64_encode($to[0]) . '?= ';
$to = implode(" <", $to);
}

if ($subject != utf8_decode($subject))
if (isset($mime_parameters))
$subject = substr(iconv_mime_encode('', $subject, $mime_parameters), 2);
else
$subject = '=?' . $charset . '?B?' . base64_encode($subject) . '?=';
}
// custom code © LWC - end

    $headers = array();
...
    if( $html )
    {
// custom code © LWC - start
if ($charset == "utf-8") {
$boundary = md5(uniqid(time()));
        $headers['Content-Type'] = 'multipart/alternative;' . "\r\n\t" . 'boundary="' . $boundary . '"';
$message_type = array(
'plain' => strip_tags($message),
'html' => '<meta http-equiv="Content-Type" content="text/html; charset=' .$charset .'">' .
"\r\n\r\n" . $message
);
$search = array('format', 'message');
$message_part = '--' . $boundary . '
Content-Type: text/' . $search[0] . '; charset=' . $charset . "\r\n\r\n" . $search[1] . "\r\n\r\n";
$message_temp = '';
foreach ($message_type as $key => $value) {
$replace = array($key, $value);
$message_temp .= str_replace($search, $replace, $message_part);
}
$message_temp .= '--' . $boundary . '--';
$message = $message_temp;
} else
// custom code © LWC - end
        $headers['Content-Type'] = 'text/html; charset=' . $charset;
 
 Quote

All times are EDT. The time is now 11:28 am.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content