Chris
Chris

Reputation: 53

Mail sent using PHP form showing garbled Japanese text

I've searched heaps on the net but nothing seems to make my code work. My php code results in emails looking like this:

叹涟: Bob メッセ〖ジ: 銇撱倱銇仭銇��

They should look like this (with Japanese characters):

名前: Bob メッセージ: こんいちは。

It works fine when entering English characters into the fields:

名前: Chris メッセージ: Hello, this is a test message.

Here's the code. The website page itself is encoded as euc-jp. The email subject shows without problems. The text is garbled in my Mac mail app, and on my android devices (gmail app, android mail app). Maybe it's a problem with the htmlspecialchars function. I don't know enough about php to fix it. Please help me!

<?php
// Check for empty fields
if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }

$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$message = strip_tags(htmlspecialchars($_POST['message']));


$to = "[email protected]\r\n";
$email_subject = "お問い合わせ\r\n";
$email_body = "名前: $name<br><br>メッセージ: $message\r\n";
$headers = "From: [email protected]\r\n";
$headers .= "Reply-To: $email_address\r\n";
$headers .= "Content-Type:text/html; charset=euc-jp\r\n";
mail($to,'=?euc-jp?B?'.base64_encode($email_subject).'?=',$email_body,$headers);
return true;            
?>

Updated code:

<?php
// Check for empty fields
if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$message = strip_tags(htmlspecialchars($_POST['message']));
$to = '[email protected]';
$email_subject = "お問い合わせ";
$email_body = "名前: $name\n\n"."メッセージ: $message";
$headers = "From: [email protected]\n";
$headers .= "Reply-To: $email_address\n";
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

Upvotes: 2

Views: 2098

Answers (4)

Shaw
Shaw

Reputation: 168

Add UTF-8 as the encoding for your email within the headers. I find using an array helps with readability, but it's up to you:

$headers = array(
  'From' => $from_address,
  'Reply-To' => $reply_address,
  'Content-Type' => 'text/plain; charset=utf-8'
);
$mailsent = mail($to_address, $subject, $body, $headers);

Make sure it's text/plain not text/html if you want any line feeds using \r\n to stay in tact once you've changed to UTF-8.

And double-check that the form submitting the POST data is doing so in UTF-8 format.

Upvotes: 0

Ryuuzaki Julio
Ryuuzaki Julio

Reputation: 379

I was having the same issue and solved by putting this on the top of my php file:

<!doctype html>
<?php header("Content-Type: text/html; charset=UTF-8"); ?>
<html>
<head>

header("Content-Type: text/html; charset=UTF-8"); Will encode all the form characters to be the ones you are looking for.

Upvotes: 1

ajay chaudhary
ajay chaudhary

Reputation: 29

This function will fix your issue:

$body_user = mb_convert_encoding($body_user, "UTF-8","UTF-8");

Upvotes: 1

Dipak Poudel
Dipak Poudel

Reputation: 69

I dont know why but your code works perfectly fine for me; I tried with:

<?php
$_POST['name']= 'Bob';
$name = $_POST['name'];

$_POST['email']= '[email protected]';
$_POST['message']= ' こんいちは';
echo $_POST['name'];

// Check for empty fields
if(empty($_POST['name'])        ||
empty($_POST['email'])       ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name'],'EUC-JP'));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$message = strip_tags(htmlspecialchars($_POST['message'],'EUC-JP'));
$to = '[email protected]';
$email_subject = "お問い合わせ";
$email_body = "名前: $name\n\n"."メッセージ: $message";
$headers = "From: [email protected]\r\n";
$headers .= "Reply-To: $email_address\n";
mail($to,$email_subject,$email_body,$headers);
echo 'email sent';
return true;   
?>

I suggest you to check your browser or computer language setting ;)

Upvotes: 1

Related Questions