php - 使用 PHP 表单发送的邮件显示日语文本乱码

标签 php character-encoding phpmailer

我在网上搜索了很多,但似乎没有什么能让我的代码工作。我的 php 代码生成的电子邮件如下所示:

叹涟:鲍勃 メッセ〖ジ: ㊇撱倱銇仭銇��

它们应该看起来像这样(带有日语字符):

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

在字段中输入英文字符时效果很好:

名前:克里斯 メッセージ:您好,这是一条测试消息。

这是代码。网站页面本身编码为 euc-jp。电子邮件主题显示没有问题。在我的 Mac 邮件应用程序和 Android 设备(gmail 应用程序、android 邮件应用程序)上,文本出现乱码。也许是 htmlspecialchars 函数的问题。我对 php 的了解不够,无法修复它。请帮助我!

<?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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="354150464175415a45505b52595c465d5e545b544f5442541b565a58" rel="noreferrer noopener nofollow">[email protected]</a>\r\n";
$email_subject = "お問い合わせ\r\n";
$email_body = "名前: $name<br><br>メッセージ: $message\r\n";
$headers = "From: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="01766463676e736c41756e71646f666d6872696a606f607b6076602f626e6c" rel="noreferrer noopener nofollow">[email protected]</a>\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;            
?>

更新的代码:

<?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 = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9febfaecebdfebf0effaf1f8f3f6ecf7f4fef1fee5fee8feb1fcf0f2" rel="noreferrer noopener nofollow">[email protected]</a>';
$email_subject = "お問い合わせ";
$email_body = "名前: $name\n\n"."メッセージ: $message";
$headers = "From: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98effdfafef7eaf5d8ecf7e8fdf6fff4f1ebf0f3f9f6f9e2f9eff9b6fbf7f5" rel="noreferrer noopener nofollow">[email protected]</a>\n";
$headers .= "Reply-To: $email_address\n";
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

最佳答案

我不知道为什么,但你的代码对我来说工作得很好; 我尝试过:

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

$_POST['email']= '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abc6cac2c7cdc4d9c9c4c9ebccc6cac2c785c8c4c6" rel="noreferrer noopener nofollow">[email protected]</a>';
$_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 = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6abbfa3aba7afaa86abbfa3aba7afaae8a5a9ab" rel="noreferrer noopener nofollow">[email protected]</a>';
$email_subject = "お問い合わせ";
$email_body = "名前: $name\n\n"."メッセージ: $message";
$headers = "From: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="23544641454c514e63574c53464d444f4a504b48424d42594254420d404c4e" rel="noreferrer noopener nofollow">[email protected]</a>\r\n";
$headers .= "Reply-To: $email_address\n";
mail($to,$email_subject,$email_body,$headers);
echo 'email sent';
return true;   
?>

我建议您检查您的浏览器或计算机语言设置;)

关于php - 使用 PHP 表单发送的邮件显示日语文本乱码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47959948/

相关文章:

php - 通过键合并(合并)2 个数组并更改结果数组中的键名

c - 从系统发送到 Unix 的命令中转义字符

encoding - base64编码: input character

smtp - Mailgun、Mandrill 和 Ses Driver,SMTP 和 phpmailer,在 Laravel 5.1 中使用哪一个?

php - 时间、C 和 PHP

php - laravel 从数据库中获取数据

php - 将 Laravel 资源合并为一个

php - 将大型数据库从 Latin1 转换为 UTF8?

php - 如何禁用 PHPMailer 中的错误消息?

php - 通过 PHPMailer 发送电子邮件时出现身份验证错误