Home > Tech Tips, Technology > Zend Framework : Zend_Mail Japanese encoding(文字化け)issue with garbled text

Zend Framework : Zend_Mail Japanese encoding(文字化け)issue with garbled text


I had a problem with Zend_Mail garbling the titles and text of all my Japanese mails. It was a simple fix just by extending the Zend_Mail class and overlaying your methods on the default class methods and changing defaults to ISO-2202-Jp which is the standard here in Japan.

Don’t forget to convert the body text which may have been pulled from form input fields before sending.

Also, make sure that you “return $this” for each of your functions to ensure that

  $mymail->setBody()->setSubject()->setFrom()->etc command chaining works (fluid interface, I think is the term).

require_once (‘Zend/Mail.php’);

class My_Jmail extends Zend_Mail {

  public function __construct($charset = 'ISO-2202-JP') {
    parent::__construct ( $charset = 'ISO-2202-JP' );

  }

  function setBodyText( $txt , $charset = null , $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE ){
    parent::setBodyText( mb_convert_encoding($txt, 'ISO-2022-JP', mb_detect_encoding($txt)) , $charset , $encoding );
    return $this;
  }

  function setSubject( $txt ){
    parent::setSubject( mb_convert_encoding($txt, 'ISO-2022-JP', mb_detect_encoding($txt)) );
    return $this;
  }
}

 

 

?>

I must confess that I know the mere basics of encoding and what goes on behind the scenes and was stumped for a while when Tomoko complained that all the mails from our website’s enquiry form on:

  http://cafe.sunnysideenglish.com

were garbled in Apple Mail.

As a curiosity, using Apple Mail, your emails will still be garbled if you choose ISO-2202-JP since Apple Mail -or something- converts the ISO-2202-JP into UTF-8.

Thus, my computer (set to English/UTF-8) read the mails fine whilst Tomoko’s computer (in Japanese Auto Detect) showed Japanese mails all garbled! Come on Apple… Get your “auto” detect house in order.

Advertisements
Categories: Tech Tips, Technology
  1. tweisbach
    September 15, 2010 at 3:18 pm

    I noticed that in your code you’re using both ISO-2202 and ISO-2022, I wonder if one or the other is a mistake.

    • nanchatte
      September 26, 2010 at 10:11 pm

      ISO-2202 is the correct one for Japanese…. THX…. Will change the code…

  2. tw
    December 28, 2010 at 11:10 am

    Actually, the encoding appears to be ISO-2022.

    http://en.wikipedia.org/wiki/ISO-2022

    It is a very common and widespread mistake. Doing a search turns up the same problem in lots of code on the Internet.

    • nanchatte
      February 10, 2011 at 6:30 pm

      Yes, the number of projects (IN JAPAN!) that fail to handle Japanese mail is quite alarming.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: