Page 1 of 1

sending japanese email

Posted: Fri Oct 22, 2004 10:15 am
by Dan_001
Greetings, developers.

My first post here is regarding sending a japanese email - I would be very grateful for your knowledge/advice.

I have a script to encode the mail properly in unicode (UTF-8), and when I use outlook to receive the email, it comes through fine. The problem occurs when it is picked up through Entourage on a Mac, or Lotus Notes - the encoding is "Automatic" and not on UTF-8, so it has to be set manually.

The mail will be sent to Japanese machines, so they will need to read it with UTF-8 encoding.

I had thought by setting the header to UTF-8, it would automatically set the encoding on the email program to UTF-8, but this does not seem to be the case.

Does anyone have any advice on this?

Here is my script:

Code: Select all

<?php

function send_japanese_mail($to, $subject, $body, $from, $from_email, $is_html_content=false) {

   $headers  = "MIME-Version: 1.0\\n" ;
   $headers .= "From: $from <$from_email>\\n";
   $headers .= "Reply-To: $from <$from_email>\\n";
 
  /* If the body  is HTML or plain text set the Content-Type header accordingly */

   if ($is_html_content) {
     $headers .= "Content-Type: text/html;charset=UTF-8\\n";
     /* turn all line breaks into BR tags */
     $body = nl2br($body);
   }
     else {
     $headers .= "Content-Type: text/plain;charset=UTF-8\\n";
   }

  /* need to convert body to same encoding as stated in Content-Type header above */

  $body = mb_convert_encoding($body, "UTF-8","UTF-8");

  /* set any sendmail params, optional ... */
  $sendmail_params  = "-f$from_email";

  /*
   The subject is actually a "header" and can/will get mangled
   if it contains non-ASCII characters. So we need to convert
   the subject to something containing only ASCII characters.

   First we convert the subject to the same encoding as the
   body, then we use mb_encode_mimeheader() to make the subject
   line all ASCII characters.
  */

   mb_language("uni");
   $subject = mb_convert_encoding($subject, "UTF-8","UTF-8");
   $subject = mb_encode_mimeheader($subject);

   mail($to, $subject, $body, $headers, $sendmail_params) or die("could not send email");
} 

$to			 = name@domain.com";
$subject		 = "japanese character test";

// japanese text in here
$body			 = "&#35430;&#12375;&#12390;&#12415;&#12390;&#12367;&#12384;&#12373;&#12356;";

$from			 = "name";
$from_email		 = "name@domain.com";
$is_html_content = false;

send_japanese_mail($to, $subject, $body, $from, $from_email, $is_html_content);

?>

Many thanks,

Dan

Posted: Fri Oct 22, 2004 1:35 pm
by qads
you could try sending the email from one of the programs above (in japanese), then compare the headers :).

Posted: Mon Oct 25, 2004 3:35 am
by Dan_001
Thanks for your input.

I have sorted out this problem, so I thought I would post the solution.

there was a mistake in teh script - a double backslash before the n: "\\n" instead of "\n". This meant the new line character wasn't recognised and the header setting the UTF-8 encoding was lost.

this works:

Code: Select all

<?php

function send_japanese_mail($to, $subject, $body, $from, $from_email, $is_html_content=false) {

   $headers  = "MIME-Version: 1.0
From: $from <$from_email>
Reply-To: $from <$from_email>
Content-Type: text/plain;charset=UTF-8\n";

  $body = mb_convert_encoding($body, "UTF-8","UTF-8");

  $sendmail_params  = "-f$from_email";

   mb_language("uni");
   $subject = mb_convert_encoding($subject, "UTF-8","UTF-8");
   $subject = mb_encode_mimeheader($subject);

   mail($to, $subject, $body, $headers, $sendmail_params) or die("could not send email");
} 

?>