CodeIgniter Forums
Japanese (multi-byte) characters & email library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Japanese (multi-byte) characters & email library (/thread-2503.html)



Japanese (multi-byte) characters & email library - El Forum - 08-09-2007

[eluser]Aaron L.[/eluser]
Hello, In my controller (encoded in UTF-8) I am attempting to send an email which contains Japanese (multibyte) characters. The body of the email comes out fine, but the subject line are just garbled characters. Any ideas/tips/hints/thoughts as to what might be going on here?


Japanese (multi-byte) characters & email library - El Forum - 08-09-2007

[eluser]座頭市[/eluser]
This has been a headache of mine for a long time....

The standard for Japanese emails is iso-2022-jp, not utf-8 - it would be best to change your email encoding and run the subject and body text through iconv(). This will solve most, but not all, mojibake problems.

For example:
Code:
$this->load->library('email');

$config['charset'] = 'iso-2022-jp';

$this->email->initialize($config);

$this->email->to('[email protected]');
$this->email->from('[email protected]');
$this->email->subject(iconv('utf-8', 'iso-2022-jp', $subject));
$this->email->message(iconv('utf-8', 'iso-2022-jp', $message));
iso-2022-jp is a 7-bit encoding as opposed to 8-bit, so often plays nicer with email clients across the board. Even Hotmail, notorious for destroying utf-8 encoded emails.


Japanese (multi-byte) characters & email library - El Forum - 08-09-2007

[eluser]Aaron L.[/eluser]
Thanks for your reply! I will try your suggestions and let you know what happens. Thanks!


Japanese (multi-byte) characters & email library - El Forum - 08-09-2007

[eluser]Aaron L.[/eluser]
Hello again, I tried using your code and unfortunately the subject line still comes out as jibberish. My partner and I tried it in a couple of different mail clients and still no luck. Any other thoughts/ideas?

Aaron


Japanese (multi-byte) characters & email library - El Forum - 08-09-2007

[eluser]座頭市[/eluser]
Yeah... like I said, it might work... ;-)

Okay... check the Content-Tranfer-Encoding of your received emails... are they 7bit or 8bit? It should be 7bit. CI seems to set the encoding by default to 8-bit (understandable as UTF-8 is the default), which might be causing the mojibake, so you'll have to hack the email core to set the encoding to 7bit.

It might be possible to use
Code:
$config['_encoding'] = '7bit';

$this->email->initialize($config);
but I've never tried it personally... I'm only dealing with Japanese and English speakers so I just hardcoded the 7bit into the header.


Japanese (multi-byte) characters & email library - El Forum - 08-09-2007

[eluser]Unknown[/eluser]
Hello

This is code for send mail in Japanese language.

$
Code:
this->load->library('email');

        mb_language("Japanese");
        mb_internal_encoding("SHIFT-JIS");
        
        $subject = 'パスワードお知らせ';
        $message = 'パスワード再発行。';
        
        $name = "管理者";
        $name=mb_convert_encoding($name,'iso-2022-jp', 'SHIFT-JIS');
        $name='=?iso-2022-jp?B?'.base64_encode($name).'?=';
        
        $subject=mb_convert_encoding($subject,'iso-2022-jp', 'SHIFT-JIS');
        $subject='=?iso-2022-jp?B?'.base64_encode($subject).'?=';
    
        $this->email->to('[email protected]');
        $this->email->from('[email protected]',$name);
        
         $this->email->subject($subject);
        $this->email->message($message);
        $this->email->send();



Japanese (multi-byte) characters & email library - El Forum - 03-14-2008

[eluser]Unknown[/eluser]
[quote author="座頭市" date="1186673766"]
$this->email->subject(iconv('utf-8', 'iso-2022-jp', $subject));
$this->email->message(iconv('utf-8', 'iso-2022-jp', $message));
[/code]
[/quote]

I think the problem is that iconv is including 'Subject:', so it's included for a second time when sending the email. I used str_replace to remove inconv's 'Subject:' and it worked fine.