Search code examples
phputf-8character-encodinginternationalizationascii

PHP and Russian Letters


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 http://lh3.ggpht.com/_1TPOP7DzY1E/S1y6Y0wb9tI/AAAAAAAAC88/OkdMQkO47HQ/s800/works.png

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 http://lh6.ggpht.com/_1TPOP7DzY1E/S1zFqFe9ohI/AAAAAAAAC9E/PZ7C4JtEHTU/s800/subject.png


Solution

  • 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?