patch for headers of email sent in non-ascii languages

This is an archive of the phpBB 2.0.x support forum. Support for phpBB2 has now ended.
Forum rules
Following phpBB2's EoL, this forum is now archived for reference purposes only.
Please see the following announcement for more information: viewtopic.php?f=14&t=1385785
Locked
roadrunner2
Registered User
Posts: 2
Joined: Sun Apr 24, 2005 3:41 am

patch for headers of email sent in non-ascii languages

Post by roadrunner2 » Sun Apr 24, 2005 5:57 am

For non-ascii languages, such as japanese, some of the headers (most notably the subject) may also contain non-ascii characters. The headers must be properly encoded in this case, but this is not currently done, resulting in garbage subjects. Following is a simple patch, against 2.0.14, which encodes all non-ascii blocks (sequences of characters between spaces). This is far from perfect, but does the job well enough for phpBB.

Code: Select all

-------------------------------------------------------------------------------
--- includes/emailer.php.orig   2004-11-18 15:02:11.000000000 -0500
+++ includes/emailer.php        2005-04-23 23:03:12.000000000 -0400
@@ -194,6 +194,14 @@
                // Build header
                $this->extra_headers = (($this->reply_to != '') ? "Reply-to: $this->reply_to\n" : '') . (($this->from != '') ? "From: $this->from\n" : "From: " . $board_config['board_email'] . "\n") . "Return-Path: " . $board_config['board_email'] . "\nMessage-ID: <" . md5(uniqid(time())) . "@" . $board_config['server_name'] . ">\nMIME-Version: 1.0\nContent-type: text/plain; charset=" . $this->encoding . "\nContent-transfer-encoding: 8bit\nDate: " . date('r', time()) . "\nX-Priority: 3\nX-MSMail-Priority: Normal\nX-Mailer: PHP\nX-MimeOLE: Produced By phpBB2\n" . $this->extra_headers . (($cc != '') ? "Cc: $cc\n" : '')  . (($bcc != '') ? "Bcc: $bcc\n" : ''); 
 
+               $res = array();
+               foreach (explode("\n", $this->extra_headers) as $hdr)
+                   array_push($res, $this->encode_nonascii($hdr));
+               $this->extra_headers = implode("\n", $res);
+
+               $to            = $this->encode_nonascii($to);
+               $this->subject = $this->encode_nonascii($this->subject);
+
                // Send message ... removed $this->encode() from subject for time being
                if ( $this->use_smtp )
                {
@@ -237,6 +245,18 @@
                return true;
        }
 
+       function encode_nonascii($str)
+       {
+               $res = array();
+               foreach (explode(' ', $str) as $blk)
+               {
+                   if (!preg_match("/^[\x21-\x7f]*$/", $blk))
+                       $blk = $this->encode($blk);
+                   array_push($res, $blk);
+               }
+               return implode(' ', $res);
+       }
+
        // Encodes the given string for proper display for this encoding ... nabbed 
        // from php.net and modified. There is an alternative encoding method which 
        // may produce lesd output but it's questionable as to its worth in this 
-------------------------------------------------------------------------------

Slackervaara
Registered User
Posts: 195
Joined: Thu Feb 28, 2008 7:46 am

Re: patch for headers of email sent in non-ascii languages

Post by Slackervaara » Thu Sep 18, 2008 4:57 pm

Thanks for a nice patch. I have tried it on phpbb 2.0.17 and it works, but there seems to be a minor bug.The words are sometimes integrated into next word like på inlägg is påinlägg. It happens when two adjecent words both contains the Swedish national characters åäö. I have used the patch in a Swedish forum, which have the Swedish national characters åäö, which are the reason for the need of the patch.

Slackervaara
Registered User
Posts: 195
Joined: Thu Feb 28, 2008 7:46 am

Re: patch for headers of email sent in non-ascii languages

Post by Slackervaara » Sun Sep 28, 2008 2:30 pm

This encodes the entire subject line to ISO-8859-1 and worked with emailer.php of 2.0.17

Find:

Code: Select all

    // Build header
          $this->extra_headers = (($this->reply_to != '') ? "Reply-to: $this->reply_to\n" : '') . (($this->from != '') ? "From: $this->from\n" : "From: " . $board_config['board_email'] . "\n") . "Return-Path: " . $board_config['board_email'] . "\nMessage-ID: <" . md5(uniqid(time())) . "@" . $board_config['server_name'] . ">\nMIME-Version: 1.0\nContent-type: text/plain; charset=" . $this->encoding . "\nContent-transfer-encoding: 8bit\nDate: " . date('r', time()) . "\nX-Priority: 3\nX-MSMail-Priority: Normal\nX-Mailer: PHP\nX-MimeOLE: Produced By phpBB2\n" . $this->extra_headers . (($cc != '') ? "Cc: $cc\n" : '')  . (($bcc != '') ? "Bcc: $bcc\n" : '');
After this a space and then add this line:

Code: Select all

          $this->subject = $this->encode($this->subject); //

Find:

Code: Select all

    // determine length of encoded text within chunks and ensure length is even
                    $length = 75 - strlen($start) - strlen($end);
                    $length = floor($length / 2) * 2;
Change to:

Code: Select all

     // determine length of encoded text within chunks and ensure length is even
                    $length = 140 - strlen($start) - strlen($end);
                    $length = floor($length / 2) * 2;
If long subject lines don't get completely coded increase the value of $length,

Locked

Return to “2.0.x Support Forum”

Who is online

Users browsing this forum: No registered users and 17 guests