Monday, April 10, 2006

Function PHP for encode Subject mail

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;
}

$email = "test@hotmail.com";
$headers = 'From: support@test' . "\r\n" . 'MIME-Version: 1.0' . "\r\n". 'Content-type: text/plain; charset=windows-874' . "\r\n".
'Reply-To: support@test' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail("$email", encode("Subject thai langusge ","windows-874"), "Body message",$headers);

?>

1 comment:

DC said...

Thanks for your code. You just saved me.