Log in

View Full Version : Resolved Problem with specials characters and php



paldo
02-13-2014, 11:24 AM
When my form is filled out with special characters I get a mail with strange characters eventhough I have set the charset to UTF-8. I made a test form as example, if you like to have a try.

Can someone tell me how I can force the form to display the characters correctly?

The problem is in the php script, line 14: (élève / élève) should display the word élève on the mail that I get.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>test specials characters</title>
</head>

<body>

After filling out this form, a mail will be sent .

<form name="test" action="test1.php" method="POST">

<div>

<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Enter
&eacute;l&egrave;ve<br />
<input type="text" name="name" size="40" /><br /></td>
</tr>
<tr>
<td>email<br />
<input type="text" name="email" size="40" /></td>
</tr>

<tr>
<td><input name="submit" type="submit" value=" send " />
</td>
</tr>
</table>

</div>
</form>
</body>
</html>


And the reply script:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$email = $_POST['email'];
$name = $_POST['name'];
// Print the message.
print " Thank you, you enter &eacute;l&egrave;ve";
// Send the email.
$body = " You have enter $name (&eacute;l&egrave;ve / élève), thank you ";
mail ("$email,mymail@gmail.com", 'test specials characters', $body);
?>
</body>
</html>

ozinder
02-13-2014, 02:47 PM
<?php
$email = $_POST['email'];
$name = $_POST['name'];
// Print the message.
print " Thank you, you enter &eacute;l&egrave;ve";
// Send the email.
$body = " You have enter $name (&eacute;l&egrave;ve / élève), thank you ";
mail ("$email,mymail@gmail.com", 'test specials characters', $body);
?>


The problem is, you are sending a pure text mail, so the & doesn't have the same meaning as in HTML. You can find a short description on sending an HTML-Mail here: css-tricks.com/sending-nice-html-email-with-php/

Good Luck!

paldo
02-13-2014, 07:59 PM
Thank you Ozinder, very useful link for a better presentation of my text mail . Unfortunately it doesn't solve my problem of special characters. My text mail is in french and in the text I have to use many special characters.

traq
02-13-2014, 08:49 PM
IF you are using utf-8, as you say, then there is no need for encoding those characters. utf-8 supports all these chartacters natively. How are you setting the character encoding?

paldo
02-13-2014, 09:10 PM
I have learned that if I'm using special characters I have to set the charset to utf-8. Doing so, my php form print the characters correctly on the browser, parse the value with special characters also correctly on the mail that I receive but the text containing special characters that I have add to the mail are not displayed correctly. I think that my php form is confused when the $name contain a special character. In fact if the entered word $name doesn't contain a special character, my mail is displayed correctly.

traq
02-14-2014, 05:07 AM
You need to be sure that the php script, the output html (the "webpage" itself) and the email are all encoded with, and declared as, utf-8.

In general, browsers encode form submissions with whatever encoding the webpage was sent in, so that's your first step. Note that many web hosts automatically send a charset declaration for you, and it's not always utf-8 (some version of ISO-8859 is very common).

Setting the charset in your HTML markup is not sufficient in most cases ([almost?] all browsers will ignore the html charset if the server sends a content-type header). If the browser is unsure about what character encoding to use, many (especially IE) will try to auto-detect it by reading the first few hundred bytes or so: because this is probably all in the <head> section of your page, it will all probably be standard latin characters (which are the same in ASCII and UTF-8, and therefore indistinguishable).

The same points apply to the email itself: make sure your php script is encoded with utf-8, and make sure to specify the charset as utf-8.

ozinder
02-14-2014, 07:27 AM
My text mail is in french and in the text I have to use many special characters.
Perhaps I missunderstand: Do you send a text mail or a HTML-Mail?

If you use HTML, you will perhaps need to replace the special chars entered in the form with their ampersand representation in HTML.
If you use UTF-8 in your form, your strings and your mail, you will need
Content-Type:text/plain;charset=utf-8 in your mail-header.

Perhaps this one (http://stackoverflow.com/questions/2265579/php-e-mail-encodinghttp://)helps you a step along.

paldo
02-14-2014, 09:16 PM
I'm stuck, no improvement. I have change the form a little bit so that you can test if you wish.
http://www.anti-aging-solutions.ch/jeunesse/test/test1.html



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>test specials characters</title>
</head>

<body>

After filling out this form, a mail will be sent to you . Try first by filling out with a word not containing a special character, and then another mail containing a special character. You will see that in the first mail my plain text that contain special character will display correctly, but not in the second mail. <br/><br/>

<form name="test" action="test1.php" method="POST">

<div>

<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Enter for example eleve in the first mail and
&eacute;l&egrave;ve in the second<br />
<input type="text" name="name" size="40" /><br /></td>
</tr>
<tr>
<td>email<br />
<input type="text" name="email" size="40" /></td>
</tr>

<tr>
<td><input name="submit" type="submit" value=" send " />
</td>
</tr>
</table>

</div>
</form>
</body>
</html>

And the php script:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$email = $_POST['email'];
$name = $_POST['name'];


// Print the message.
print " Thank you, you enter &eacute;l&egrave;ve";


// Send the email.
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$message .= " You have enter $name (élève), thank you ";


mail ("$email", 'test specials characters', $message, $headers);
?>
</body>
</html>

Thanks for your help.

traq
02-14-2014, 09:27 PM
First, your test page is not declared as UTF-8.
Content-Type: text/html; charset=ISO-8859-1
See my post above. You need to send an HTTP header with the correct charset: the charset in your html markup will not be sufficient.

Your code that send the email looks correct, except that the first header line should be assigned, not appended:
# NO
# $headers .= "MIME-Version: 1.0\r\n";

# Yes
$headers = "MIME-Version: 1.0\r\n";

paldo
02-14-2014, 09:46 PM
You mean the form page (../test1.html) is not declared UTF-8? Is <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> not the correct declaration? What else and where do I have to declare? Thanks

traq
02-14-2014, 10:46 PM
Note that many web hosts automatically send a charset declaration for you, and it's not always utf-8 (some version of ISO-8859 is very common).

Setting the charset in your HTML markup is not sufficient in most cases ([almost?] all browsers will ignore the html charset if the server sends a content-type header).


You need to send an HTTP header with the correct charset: the charset in your html markup will not be sufficient.

If you don't know what an HTTP header is, read more here (http://wikipedia.org/wiki/Hypertext_Transfer_Protocol).

HTTP ("internet") requests/responses consist of two parts: the headers and the body. An HTTP response looks something like this, for example:
HTTP/1.1 200 OK
Date: Mon, 23 May 2005 22:38:34 GMT
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
ETag: "3f80f-1b6-3e1cb03b"
Content-Type: text/html; charset=UTF-8
Content-Length: 131
Accept-Ranges: bytes
Connection: close

<html>
<head>
<title>An Example Page</title>
</head>
<body>
Hello World, this is a very simple HTML document.
</body>
</html>
The Headers are in the top portion; the Body is everything below the blank line. Note that that means all of your HTML is part of the HTTP body. It is a bit confusing, because HTML uses the same terms ("<head>" and "<body>") with html-specific meanings. So, what's happening in your case is something like this:
HTTP/1.1 200 OK
Content-Type: text/html; charset=ISO-8859-1
Other-Headers: etc..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<!-- the rest of your html ... -->
See how the HTTP headers say one thing ("ISO-8859-1") and the HTTP body says something else ("UTF-8", in your <meta> tag)? Almost every browser (indeed, every browser period, as far as I know) will believe the HTTP header, and ignore the meta tag. The only time specifying the charset in your html will have any effect is when no corresponding HTTP header is sent—in fact, that's the situation they were designed for.

Since your server is automatically setting a header with the wrong charset, you need to specify the correct one. That's exactly what the header (http://php.net/header) function does.
<?php

header( "Content-type: text/html; charset=UTF-8" );

// etc. ...

paldo
02-16-2014, 07:35 PM
I have changed the charset in the meta tag to charset=ISO-8859-1 and all problems are gone !

traq
02-16-2014, 11:00 PM
I have changed the charset in the meta tag to charset=ISO-8859-1 and all problems are gone !

Glad to hear it. I would still suggest considering UTF-8: it includes nearly every character of every written language, is highly interoperable, and has become the de-facto standard in the computing world. In the long run, it's the better choice.

If your question has been answered, please mark your thread "resolved":
On your original post (post #1), click [edit], then click [go advanced]. In the "thread prefix" box, select "Resolved". Click [save changes].

paldo
02-17-2014, 09:02 AM
For the moment being I will leave the script in charset=ISO-8859-1 until I have understood the theory behind charsets and their implementation in a php script. Thanks for your help.