简体   繁体   中英

French characters not displaying correctly in PHP mail

I have a basic PHP mail form, but whatever I do, I cannot seem to get the characters to display correctly once sent, if the language is written with French accents.

The example sentence I am using is:

Bonjour, ceci est un message de test envoyé avec PHP pour analyser si oui ou non la mise en forme est correcte ou fausse. Les personnages ne devraient être rendus de manière appropriée comme prévu dans le lexique français.

But it comes out as:

Bonjour, ceci est un message de test envoyé avec PHP pour analyser si oui ou non la mise en forme est correcte ou fausse. Les personnages ne devraient être rendus de manière appropriée comme prévu dans le lexique français.

As you can see, the characters with accents are screwed up, once they are received in the email.

I am processing my message variable as such:

$fieldenquiry = utf8_encode($_POST['fieldenquiry']);

I am then, sending it like so:

$cc = "example@example.com";
$subject = "Website Enquiry";

$message = '<html><body>';
$message .= "<p><strong>Enquiry</strong><br />" . nl2br($fieldenquiry) . "</p>";
$message .= "</body></html>";

$headers = "From: " . $fieldemail . "\r\n";
$headers .= "Cc: " . $cc . "\r\n";
$headers .= "Reply-To: ". $fieldemail . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

mail($to, $subject, $message, $headers);

I'm not a PHP developer by any means. The form works in the sense that it sends etc, but I cannot figure out why the characters mess up. I am encoding the POST variable and I am sending the HTML format with a UTF-8 charset.

Help and guidance appreciated.

Michael.

EDIT:

I figured this out. See my answer below.

I figured it out if anyone is in need of a similar piece of help:

I changed this line:

$fieldenquiry = utf8_encode($_POST['fieldenquiry']);

To this:

$fieldenquiry = utf8_encode(htmlentities($_POST['fieldenquiry'], ENT_QUOTES, "UTF-8"));

I use the htmlentities() function with UTF-8 specified in the arguments.

This fixed the issue completely. Hope it helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM