1

I have a contact form on my website which relies on PHP to do it's job. The problem is that when you send a message written with cyrillic characters I only get a bunch of question marks.

Here's the code of that form below:

<?php header('Content-Type: text/html; charset=utf-8');
      header('Content-Transfer-Encoding: 8bit');
if(isset($_POST['email'])){
        $mailTo = "ameli_cakes@abv.bg";
        $subject = "mail from web";
        $body = "New message from web
<br><br>
FROM: ".$_POST['email']."<br>
NAME: ".$_POST['name']."<br>
SUBJECT: ".$_POST['subject']."<br>
COMMENTS: ".$_POST['message']."<br>";   
        $headers = "To: Ameli Cakes <".$mailTo.">\r\n";
        $headers .= "From: ".$_POST['author']." <".$_POST['email'].">\r\n";
        $headers .= 'Content-Type: text/HTML; charset=utf-8' . "\r\n";
        $headers .= 'Content-Transfer-Encoding: 8bit'. "\n\r\n";
        //envio destinatario
        $mail_success =  mail($mailTo, $subject, $body, $headers);      
}
?>

Keep in mind that my knowledge of PHP is very limited and I'm not the author of this piece of code.

The HTML of the page has the appropriate meta tag I believe, you can check it below:

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

The PHP file itself is encoded as utf-8 too (checked with notepad++).

Below is an example of what happens:

I sent using the form the message

This is non Cyrillic text, now follows some Cyrillic text. Поздрави другар!

And I receive

This is non Cyrillic text, now follows some Cyrillic text. Поздрави другар!!

I would truly appreciate any help you can give me in solving this, thank you very much for your time in reading this.

PS: IT'S FIXED!! check the code above to see how it finally worked.

Nipponshin
  • 23
  • 1
  • 8
  • Possible duplicate of [PHP E-Mail Encoding?](http://stackoverflow.com/questions/2265579/php-e-mail-encoding) – CBroe Oct 05 '15 at 22:07

2 Answers2

2

It's important that your entire code has the same charset to avoid issues where characters displays incorrectly.

There are a few settings that needs to be properly defined, and I'd strongly recommend UTF-8 (as you already set in your HTML), as this has most letters you would need (Scandinavian, Greek, Arabic).

Below I've made a little list of things that has to be set to a specific charset.

Headers

  • Setting the charset in both HTML and PHP headers to UTF-8

    • PHP: header('Content-Type: text/html; charset=utf-8');
      (PHP headers has to be placed before any output (echo, whitespace, HTML)!)

    • HTML: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      (HTML-headers are placed within the <head> / </head> tag)

File-encoding

  • It's also important that the .php-file itself is UTF-8 encoded. If you're using Notepad++ to write your code, this can be done in the "Format" drop-down on the taskbar.

You can take a look at this StackOverflow post: UTF-8 all the way through.

Community
  • 1
  • 1
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • Hi, thanks for the answer, I updated the post with your advices. The PHP code has been updated with the header but the issue persists, meta tag in HTML is inside of head tag and the PHP file is encoded as utf-8. checked with notepad++. – Nipponshin Oct 05 '15 at 21:06
  • You should also remove `utf8_decode()`. [The manual](http://php.net/manual/en/function.utf8-decode.php) states that it converts from UTF-8 to *single-byte ISO-8859-1*. Also, is your file encoded with `UTF-8 w/o BOM` or `UTF-8`? It should be the first (without BOM). – Qirel Oct 05 '15 at 21:19
  • Notepad++ says it's UTF-8 w/o BOM, I updated the code as suggested and still does not work but there is a change. The question marks disappeared and they've been replaced by this "Поздрави другар!", it's still not what it should be (check original example), thanks! – Nipponshin Oct 05 '15 at 21:20
  • If you `echo $_POST['email'];` (before it's sent), is it properly encoded? Because it should be if your headers are set and file is properly encoded. – Qirel Oct 05 '15 at 21:27
  • I'm not sure what you're asking me to do... My PHP knowledge is really bad, sorry. Should I add that on the code somewhere? I get syntax error when I do. – Nipponshin Oct 05 '15 at 21:34
  • Place `echo $_POST['message'];` above `$mailTo = "ameli_cakes@abv.bg";`, for an example (if you get a syntax error, you're doing something wrong). And then send a mail - you should see the echo, and it should be properly encoded. If it's not, I wouldn't know why (as headers and file are encoded, as you claim). – Qirel Oct 05 '15 at 21:37
  • Hi, I added the code like you said and I get no syntax errors but I see no "echo" when I use the form to send an email, where this should appear? – Nipponshin Oct 05 '15 at 21:51
  • Echo means it'll print the text for the browser, and if you want to figure out where, put some text to it, like `echo "Message from POST is: ".$_POST['message'];` and it should be where "Message from POST" is on your website, after you sent the mail. – Qirel Oct 05 '15 at 21:55
  • HI, I updated the code like you said but I really can't see any "echo", or I'm just too dumb to realize what I'm supposed to do :/. My website is amelicakes.com in case you want to check it out. (check original post to see how the PHP file looks like right now). – Nipponshin Oct 05 '15 at 22:06
  • `echo` means "print text". So it should be somewhere in your browser when you send the mail (look for the extra text if you can't find it directly). – Qirel Oct 05 '15 at 22:49
  • Is fixed, thanks so much for the help! Check the code in the original post to see how it looks now that it works :). – Nipponshin Oct 05 '15 at 23:04
1

You need two headers in Your eMail.

Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit

It would be something like this in Your code:

$headers .= "Content-Type: text/html; charset=utf-8";
$headers .= "Content-Transfer-Encoding: 8bit";

P.S.
The stream

D0 9F D0 BE D0 B7 D0 B4 D1 80 D0 B0 D0 B2 D0 B8 20 D0 B4 D1 80 D1 83 D0 B3 D0 B0 D1 80 21 21

Поздрави другар!! ← as UTF-8

Поздрави другар!! ← as Windows-1251

Michas
  • 8,534
  • 6
  • 38
  • 62
  • @qirel I added the second header like this: `header('Content-Type: text/html; charset=utf-8'); header('Content-Transfer-Encoding: 8bit');` But the issue persists, thank you for helping! – Nipponshin Oct 05 '15 at 22:24
  • I get this on an email. I do not get question marks anymore after following @Qirel advices, now I get "Поздрави другар!" when I should get "Поздрави другар!". after removing the utf8_decode() function. I will update the post to reflect this. – Nipponshin Oct 05 '15 at 22:37
  • These was eMail headers. If You use the `mail` function, the `header` don't set anything in an eMail. – Michas Oct 05 '15 at 22:53
  • Is fixed, thanks for the help! Check the code in the original post to see how it looks now that it works. @Qirel thanks so much for all your help too! – Nipponshin Oct 05 '15 at 23:02