By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

How to send subject in chinese using mail()

P: 3
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
Dec 7 '07 #1
Share this Question
Share on Google+
5 Replies


P: 41
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@example.com';
$subject = 'some chinese text'
//this is the encoded subject;
$encoded_subject = "=?$charset?B?".base64_encode($subject)."?=\n" ;
$message = 'some chinese text';

//HEADERS
$header .= "X-Mailer: Whatever you want\n";
$header .= "Return-Path: <someperson@example.com>\n";
$header .= "MIME-Version: 1.0\n";
$header .= "From: Some Person <someperson@example.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={$charset}\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_subject, $message, $header)) {
echo "Error: Message not sent";
}
else {
echo "Message sent successfully!";
};
?>
[/PHP]

The main answer to your question lies in the $encoded_subject 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
Dec 9 '07 #2

P: 5
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.
Dec 10 '07 #3

P: 41
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.
Dec 10 '07 #4

P: 3
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@example.com';
$subject = 'some chinese text'
//this is the encoded subject;
$encoded_subject = "=?$charset?B?".base64_encode($subject)."?=\n" ;
$message = 'some chinese text';

//HEADERS
$header .= "X-Mailer: Whatever you want\n";
$header .= "Return-Path: <someperson@example.com>\n";
$header .= "MIME-Version: 1.0\n";
$header .= "From: Some Person <someperson@example.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={$charset}\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_subject, $message, $header)) {
echo "Error: Message not sent";
}
else {
echo "Message sent successfully!";
};
?>
[/PHP]

The main answer to your question lies in the $encoded_subject 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]
Dec 10 '07 #5

P: 41
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.
Dec 10 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.