Author Topic: SVN 286 character encoding issue  (Read 5194 times)

Offline rene.s

  • Newbie
  • *
  • Posts: 2
SVN 286 character encoding issue
« on: August 01, 2006, 01:25:36 AM »
There seems to be a bug in charater encoding of the From:, To: and CC: header field.
Setting a non-ASCI Character under Identities, (e.g André, Müller,...) or even in the Adress Book breaks the encoding.

Example:

Setting André under Identities as sender name results in:

From: =?UTF-8?Q?Andr=C3=A9=20?=

Correct would be:

From: =?UTF-8?Q?Andr=C3=A9=20?=


René

Offline rjsalamanca

  • Newbie
  • *
  • Posts: 3
Re: SVN 286 character encoding issue
« Reply #1 on: August 27, 2006, 12:20:59 AM »
I have the same problem. I commented out line 57 at /program/steps/mail/sendmail.inc but now I am sending mails an the sender's name does not appears, only the sender's address.

How can I put the sender's name?

Offline rene.s

  • Newbie
  • *
  • Posts: 2
Re: SVN 286 character encoding issue
« Reply #2 on: September 12, 2006, 07:43:06 AM »
Same bug is still in SVN Rev. 327 :-[

At the moment I'm using rjsalamanca's workaround:

To resolve the issue with the "From:"-field:

Code: [Select]
56: $out['string'] = sprintf('%s <%s>',
57:  rcube_charset_convert($name, $CHARSET, $OUTPUT->get_charset()),
58:  $sql_arr['mailto']);

relace with

Code: [Select]
56: $out['string'] = sprintf('%s <%s>',
57:  $sql_arr['mailto'],
58:  $sql_arr['mailto']);


To resolve the issue with the "To:"-field:

Code: [Select]
112: $headers = array('Date' => date('D, j M Y G:i:s O'),
113: 'From' => $identity_arr['string'],
114: 'To'  => rcube_charset_convert($mailto, $input_charset, $message_charset));

Code: [Select]
112: $headers = array('Date' => date('D, j M Y G:i:s O'),
113: 'From' => $identity_arr['string'],
114: 'To'  => $mailto);

Offline Arabian

  • Jr. Member
  • **
  • Posts: 10
Re: SVN 286 character encoding issue
« Reply #3 on: October 04, 2006, 07:14:33 PM »
I wish this could fixed soon too, I have the same problem with the Arabic chars windows-1256.

Offline enyone

  • Newbie
  • *
  • Posts: 2
Re: SVN 286 character encoding issue
« Reply #4 on: October 18, 2006, 12:23:54 PM »
The problem is in file "program/lib/Mail/Mime.php" in local function "_encodeHeaders" (starts from line 788)..
It seems to be common error in regular expressions.. (I hate em..)

I'm solving this issue, but I don't know when I'm done.. Maybe I'll have some free time in next few days, maybe.. ;)


So see you soon,.. or maybe not ;)


Code snippet (function _encodeHeaders):
Code: [Select]
 function _encodeHeaders($input)
  {
    foreach ($input as $hdr_name => $hdr_value) {
      if (function_exists('iconv_mime_encode') && preg_match('#[\x80-\xFF]{1}#', $hdr_value)){
        $imePref = array();
        if ($this->_build_params['head_encoding'] == 'base64'){
          $imePrefs['scheme'] = 'B';
        }else{
          $imePrefs['scheme'] = 'Q';
        }
        $imePrefs['input-charset'] = $this->_build_params['head_charset'];
        $imePrefs['output-charset'] = $this->_build_params['head_charset'];
        $hdr_value = iconv_mime_encode($hdr_name, $hdr_value, $imePrefs);
        $hdr_value = preg_replace(&quot;#^{$hdr_name}\:\ #&quot;, &quot;&quot;, $hdr_value);
      }elseif (preg_match('#[\x80-\xFF]{1}#', $hdr_value)){
        //This header contains non ASCII chars and should be encoded.
        switch ($this->_build_params['head_encoding']) {
        case 'base64':
          //Base64 encoding has been selected.
         
          //Generate the header using the specified params and dynamicly
          //determine the maximum length of such strings.
          //75 is the value specified in the RFC. The -2 is there so
          //the later regexp doesn't break any of the translated chars.
          $prefix = '=?' . $this->_build_params['head_charset'] . '?B?';
          $suffix = '?=';
          $maxLength = 75 - strlen($prefix . $suffix) - 2;
          $maxLength1stLine = $maxLength - strlen($hdr_name);
         
          //Base64 encode the entire string
          $hdr_value = base64_encode($hdr_value);

          //This regexp will break base64-encoded text at every
          //$maxLength but will not break any encoded letters.
          $reg1st = &quot;|.{0,$maxLength1stLine}[^\=][^\=]|&quot;;
          $reg2nd = &quot;|.{0,$maxLength}[^\=][^\=]|&quot;;
          break;
        case 'quoted-printable':
        default:
          //quoted-printable encoding has been selected
         
          //Generate the header using the specified params and dynamicly
          //determine the maximum length of such strings.
          //75 is the value specified in the RFC. The -2 is there so
          //the later regexp doesn't break any of the translated chars.
          $prefix = '=?' . $this->_build_params['head_charset'] . '?Q?';
          $suffix = '?=';
          $maxLength = 75 - strlen($prefix . $suffix) - 2;
          $maxLength1stLine = $maxLength - strlen($hdr_name);
         
          //Replace all special characters used by the encoder.
          $search = array(&quot;=&quot;,  &quot;_&quot;,  &quot;?&quot;,  &quot; &quot;);
          $replace = array(&quot;=3D&quot;, &quot;=5F&quot;, &quot;=3F&quot;, &quot;_&quot;);
          $hdr_value = str_replace($search, $replace, $hdr_value);
         
          //Replace all extended characters (\x80-xFF) with their
          //ASCII values.
          $hdr_value = preg_replace(
            '#([\x80-\xFF])#e',
            '&quot;=&quot; . strtoupper(dechex(ord(&quot;\1&quot;)))',
            $hdr_value
          );
          //This regexp will break QP-encoded text at every $maxLength
          //but will not break any encoded letters.
          $reg1st = &quot;|(.{0,$maxLength})[^\=]|&quot;;
          $reg2nd = &quot;|(.{0,$maxLength})[^\=]|&quot;;
          break;
        }
        //Begin with the regexp for the first line.
        $reg = $reg1st;
        $output = &quot;&quot;;
        while ($hdr_value) {
          //Split translated string at every $maxLength
          //But make sure not to break any translated chars.
          $found = preg_match($reg, $hdr_value, $matches);
         
          //After this first line, we need to use a different
          //regexp for the first line.
          $reg = $reg2nd;

          //Save the found part and encapsulate it in the
          //prefix & suffix. Then remove the part from the
          //$hdr_value variable.
          if ($found){
            $part = $matches[0];
            $hdr_value = substr($hdr_value, strlen($matches[0]));
          }else{
            $part = $hdr_value;
            $hdr_value = &quot;&quot;;
          }
         
          //RFC 2047 specifies that any split header should be seperated
          //by a CRLF SPACE.
          if ($output){
            $output .= &quot;\r\n &quot;;
          }
          $output .= $prefix . $part . $suffix;
        }
        $hdr_value = $output;
      }
      $input[$hdr_name] = $hdr_value;
    }

    return $input;
  }

Offline enyone

  • Newbie
  • *
  • Posts: 2
Re: SVN 286 character encoding issue
« Reply #5 on: October 18, 2006, 01:34:34 PM »
Temporary fix!

Quote
The problem is in file "program/lib/Mail/Mime.php" in local function "_encodeHeaders" (starts from line 788)..

This fix allows you to use only one (1) recipient !

OLD (starts from line 879):
Code: [Select]
         //RFC 2047 specifies that any split header should be seperated
          //by a CRLF SPACE.
          if ($output){
            $output .= &quot;\r\n &quot;;
          }
          $output .= $prefix . $part . $suffix;
        }
        $hdr_value = $output;
      }
      $input[$hdr_name] = $hdr_value;
    }

NEW (starts from line 879):
Code: [Select]
         //RFC 2047 specifies that any split header should be seperated
          //by a CRLF SPACE.
          if ($output){
            $output .= "\r\n ";
          }
          $output .= $prefix . $part . $suffix;
          $output = str_replace(">?=", ">", $output);
          $output = str_replace("_<", "?= <", $output);
        }
        $hdr_value = $output;
      }
      $input[$hdr_name] = $hdr_value;
    }

see above and replace your code ^^

If somebody is able to solve this issue completely, go ahead! ;)