character encoding - Mail sent using PHP form showing garbled Japanese text
Get the solution ↓↓↓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;
?>
Answer
Solution:
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 ;)
Answer
Solution:
This function will fix your issue:
$body_user = mb_convert_encoding($body_user, "UTF-8","UTF-8");
Answer
Solution:
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>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: sqlstate[hy000] [1698] access denied for user 'root'@'localhost'
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.