views:

312

answers:

3

What is happening with Russian letters when sending via PHP request to ... a mail, by e.g.? the "hardcoded" russians letters are displayed properly, but from the Form's textboxex with hieroglyphs:

HTML page:

<tr>
 <td style="width: 280px">Содержание работ</td>
 <td><input type="text" id="workContent"/></td>
</tr>

PHP page:

$WorkContent = $_REQUEST["workContent"]; //Содержание работ
// ...
$WorkContentLabel = "Содержание работ";
// ...
$message .= $WorkContentLabel . ":\t" . $WorkContent . "\n";
// ...
// email stuff (data below changed)
$to = "[email protected]";
$from = "[email protected]";
$from_header = "From: Russian site command ";

$subject = "Message with russian letters";
$subject = '=?utf-8?B?'.$subject.'?=';
$message .= $subject;

// send message
mail($to, $subject, $message, $from_header);

User enter some content in the textbox: alt text

and the submits the form.

What do I receive (in GMAIL):

Содержание работ:       1)Содержание 2)RABOT

So, hard-coded Russian text - OK, sent by the form Russian text - NOK, sent by the form ASCII text - OK.

Does somebody know what could be the cause of that strange behavior with the encoding?

EDIT: used

$subject = "  оборудования  - subject with russian letters";
$subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';
$message .= $subject;

obtains a subject like �����������ÿ - subject with russian letters

+1  A: 

Check your encodings:

  1. HTML encoding (in the <meta http-equiv..> tag)
  2. PHP/HTML/template file encoding (what encoding your editor saves the file in)
  3. Database encoding (if applicable) (in what encoding the data in the tables is in)
  4. Database connection encoding (if applicable) (what encoding is used for database connections)

and use UTF-8 for everything.

Nouveau
Don't forget the e-mail header as well.
Michael Madsen
+2  A: 

You need to base64_encode() your $subject, like this:

$subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';

Make sure you're also saving your .php file encoded as UTF-8 no BOM.


This question might also interest you: Is this the correct way to send email with PHP?

Alix Axel
saved the PHP document as UTF-8 with `$subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';` now everything works!Thanks a lot!
serhio
A: 

As well as what Alix said about base64 in the RFC2047 encoded-word in your Subject line, you also need to tell the mailer to expect UTF-8-encoded text in the body of the mail, by adding headers:

MIME-Version: 1.0
Content-Type: text/plain;charset=utf-8

otherwise it's up to the mailer to guess, probably wrongly.

bobince
body of the mail, do you mean the `$message`? how can I add this headers?
serhio
Yes, the `$message` is in the body. You add headers with the `additional_headers` argument, the same way you're setting `From:`. eg. `$headers= "From: Russian site command\r\nMIME-Version: 1.0\r\nContent-Type: text/plain;charset=utf-8"`.
bobince