2005
Just now encountered a problem while using PHP to send out email with Japanese subject line. The Japanese subject line is not properly encoded although I have set the encoding in the email header:
-
$headers = “MIME-Version: 1.0\r\n”;
$headers .= “Content-type: text/html; charset=Shift_JIS\r\n”;
I searched the Google and found something useful.
**********************************************
Subject line text sent as plain ShiftJIS, JIS, or EUC may be read by some mail software. However, this is not a reliable method of reading and sending the subject lines.
The standardized and recommended method to send subject lines is to convert the subject line into Base64, the same encoding method used for MIME messages. In addition to encoding the subject line as Base64, the character encoding method should be defined and a series of ? delimiters are used to seperate the parts of a subject line.
A raw mail header looks like this:
-
Subject: =?iso-2022-jp?B?GyRCJDMkbCRPO2QkTiVGJTklSCRHJDkhIxsoQg==?==?iso-2022-jp?B?IBskQkZ8S1w4bBsoQg==?=
The mail software scans the header and picks out the Subject: keyword. Next, the =? characters signal the beginning of the message. The iso-2022-jp identifies the encoding type of the text of the subject line. This is usually the same as the body of the message. However, it does not have to be. The next ? character is a delimiter. The B identifies the encoding method of the iso-2022-jp encoded message as Base64. The subject is encoded twice. First, the Japanese characters are encoded into iso-2022-jp. Then the iso-2022-jp message is encoded with Base64. After the next ? the text of the subject continues until the ending ?=. Long subjects are split into smaller sections and each section contains the start and stop delimiters, character encoding specification and transport encoding specification.
**********************************************
So here is the solution: to encode the Japanese subject line into Base64 using this function:
-
< ?php
$charset = "iso-2202-jp"; // japanese
$to = encode("japanese name 01", $charset) . "
$from = encode(”japanese name 02″, $charset) . ”
$subject = encode(”japanese text”);
$message = “does not need to be encoded”;
mail($to, $subject, $message, $from);
function encode($in_str, $charset) {
$out_str = $in_str;
if ($out_str && $charset) {
// define start delimimter, end delimiter and spacer
$end = “?=”;
$start = “=?” . $charset . “?B?”;
$spacer = $end . “\r\n ” . $start;
// determine length of encoded text within chunks
// and ensure length is even
$length = 75 - strlen($start) - strlen($end);
$length = floor($length/2) * 2;
// encode the string and split it into chunks
// with spacers after each chunk
$out_str = base64_encode($out_str);
$out_str = chunk_split($out_str, $length, $spacer);
// remove trailing spacer and
// add start and end delimiters
$spacer = preg_quote($spacer);
$out_str = preg_replace(”/” . $spacer . “$/”, “”, $out_str);
$out_str = $start . $out_str . $end;
}
return $out_str;
}
// for details on Message Header Extensions
// for Non-ASCII Text see …
// http://www.faqs.org/rfcs/rfc2047.html
?>
The problem solved.


Recent Comments