How to send subject in chinese using mail()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • serena9131
    New Member
    • Dec 2007
    • 3

    How to send subject in chinese using mail()

    Dear all,

    I having problem for sending subject in cheses language using mail(), Can anyone let me know how to send send non unicode subject ?


    Thanks
    Serena
  • clai83
    New Member
    • Dec 2007
    • 41

    #2
    Originally posted by serena9131
    Dear all,

    I having problem for sending subject in cheses language using mail(), Can anyone let me know how to send send non unicode subject ?


    Thanks
    Serena

    Did you try changing the header of the message when using the mail() function?

    I found some information at PHP.net under the mail section.

    Mail header fields and subject allow only ascii characters. You have to encode the subject line to ascii characters from your encoding yourself. You can use Quoted prinables "Q", or "B" base64.

    Base64 is easier, and PHP has a built in function

    [PHP]
    <?php
    //specify a character set
    $charset = "GB2312";
    $to = 'example@exampl e.com';
    $subject = 'some chinese text'
    //this is the encoded subject;
    $encoded_subjec t = "=?$charset?B?" .base64_encode( $subject)."?=\n ";
    $message = 'some chinese text';

    //HEADERS
    $header .= "X-Mailer: Whatever you want\n";
    $header .= "Return-Path: <someperson@exa mple.com>\n";
    $header .= "MIME-Version: 1.0\n";
    $header .= "From: Some Person <someperson@exa mple.com>\n";

    //I believe that language code for chinese is cn
    $header .= "X-Accept-Language: cn\n";

    //This header should make sure that the message has a Guobiao
    //encoding
    $header .= "Content-Type: text/plain; charset={$chars et}\n";

    //I'm not an expert, but depending on the character set you use
    //you may want to change the transfer encoding.
    //I found that GB2312 is 7 bit, so I believe you can
    //leave this header out. If you choose another encoding
    //make sure the transfer encoding is correct.
    //$header .= "Content-Transfer-Encoding: 8bit\n";

    //Print error if mail doesn't work, otherwise print successful message
    if(!mail($to, $encoded_subjec t, $message, $header)) {
    echo "Error: Message not sent";
    }
    else {
    echo "Message sent successfully!";
    };
    ?>
    [/PHP]

    The main answer to your question lies in the $encoded_subjec t variable.

    =?$charset?B?". base64_encode($ subject)."?=\n

    Just specify the character set you want and use the base64_encode function to encode your $subject.

    I do a lot of Japanese pages, and I tested a similar version of this script with a japanese encoding and it came out fine.

    Hopefully this helps, and let me know if it works

    Comment

    • rathour
      New Member
      • Sep 2007
      • 5

      #3
      try changing the charset to utf-8 of your web page

      and a better method i personally feel is that
      Ms. Frontpage provides better grip in unicode than dreamweaver.

      Comment

      • clai83
        New Member
        • Dec 2007
        • 41

        #4
        Originally posted by rathour
        try changing the charset to utf-8 of your web page

        and a better method i personally feel is that
        Ms. Frontpage provides better grip in unicode than dreamweaver.
        Did the person ask how to send a NON unicode subject? Changing the character set of the web page won't really do anything in this case. And I'm not really sure if we are even talking about a webpage either. We are talking about the mail() function.

        Anyhow all email headers must be made up of characters of the ascii character set. Chinese is not a part of that character set, thus you have to use a work around for all headers that you need to be Chinese or any language other than English for that matter.

        Of course doing everything in unicode is much easier because you only have to worry about one character set. It's getting there.

        Comment

        • serena9131
          New Member
          • Dec 2007
          • 3

          #5
          Thanks, but i got error as below

          Warning: mail(): Bad parameters to mail() function, mail not sent. in /home/content/s/e/r/serena9131/html/test_send6.php on line 31










          I found some information at PHP.net under the mail section.

          Mail header fields and subject allow only ascii characters. You have to encode the subject line to ascii characters from your encoding yourself. You can use Quoted prinables "Q", or "B" base64.

          Base64 is easier, and PHP has a built in function

          [PHP]
          <?php
          //specify a character set
          $charset = "GB2312";
          $to = 'example@exampl e.com';
          $subject = 'some chinese text'
          //this is the encoded subject;
          $encoded_subjec t = "=?$charset?B?" .base64_encode( $subject)."?=\n ";
          $message = 'some chinese text';

          //HEADERS
          $header .= "X-Mailer: Whatever you want\n";
          $header .= "Return-Path: <someperson@exa mple.com>\n";
          $header .= "MIME-Version: 1.0\n";
          $header .= "From: Some Person <someperson@exa mple.com>\n";

          //I believe that language code for chinese is cn
          $header .= "X-Accept-Language: cn\n";

          //This header should make sure that the message has a Guobiao
          //encoding
          $header .= "Content-Type: text/plain; charset={$chars et}\n";

          //I'm not an expert, but depending on the character set you use
          //you may want to change the transfer encoding.
          //I found that GB2312 is 7 bit, so I believe you can
          //leave this header out. If you choose another encoding
          //make sure the transfer encoding is correct.
          //$header .= "Content-Transfer-Encoding: 8bit\n";

          //Print error if mail doesn't work, otherwise print successful message
          if(!mail($to, $encoded_subjec t, $message, $header)) {
          echo "Error: Message not sent";
          }
          else {
          echo "Message sent successfully!";
          };
          ?>
          [/PHP]

          The main answer to your question lies in the $encoded_subjec t variable.

          =?$charset?B?". base64_encode($ subject)."?=\n

          Just specify the character set you want and use the base64_encode function to encode your $subject.

          I do a lot of Japanese pages, and I tested a similar version of this script with a japanese encoding and it came out fine.

          Hopefully this helps, and let me know if it works[/QUOTE]

          Comment

          • clai83
            New Member
            • Dec 2007
            • 41

            #6
            Originally posted by serena9131
            Thanks, but i got error as below

            Warning: mail(): Bad parameters to mail() function, mail not sent. in /home/content/s/e/r/serena9131/html/test_send6.php on line 31
            I had an error on my previous post on line 5 where I forgot a semi-colon, but other than that, I tested it and the code on the post works.

            Please post the code you tried. The error says that you didn't put in the correct variables. Check if you put it in the correct order.

            Comment

            Working...