这个问题在这里已有答案:
我的网站上有一个PHP表单。它从HTML输入元素收集文本字符串,然后将文本发送到我的邮箱。但是,当用户输入其他语言的文本时,电子邮件会显示乱码。文本在Web浏览器中似乎没问题。甚至有一些JavaScript代码可以获取/设置其中一些表单字段,并且似乎正确地执行了这些操作。
也许PHP代码不将文本视为unicode?或者使用错误的编码将文本发送到我的邮箱?谢谢,我将不胜感激。
我已经添加了
header("Content-Type: text/html; charset=utf8");
到PHP文件的顶部。
这是PHP表单的一部分,它使用POST获取输入的值。
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$titletxt = $_POST["titletxt"];
$titleurl = $_POST["titleurl"];
$legend = $_POST["legend"];
$command = $_POST["command"];
$binding = $_POST["binding"];
$layout = $_POST["layout"];
以下是PHP表单的一部分,通过电子邮件向我发送文本:
mail
(
"[email protected]",
"VGKD Bindings Submission",
"NAME:\t\t" . $name . "\n" .
"EMAIL:\t\t" . $email . "\n" .
"MESSAGE:\t" . $message . "\n" .
"GAME TITLE:\t" . $titletxt . "\n" .
"GAME URL:\t" . $titleurl . "\n" .
"LAYOUT:\t\t" . $layout . "\n\n" .
"LEGENDS:\n" . $legend . "\n\n" .
"COMMANDS:\n" . $command . "\n\n" .
"BINDINGS:\n" . $binding . "\n\n"
);
例如,请尝试在邮件功能中添加utf-8字符集标题
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
mail
(
"[email protected]",
"VGKD Bindings Submission",
"NAME:\t\t" . $name . "\n" .
"EMAIL:\t\t" . $email . "\n" .
"MESSAGE:\t" . $message . "\n" .
"GAME TITLE:\t" . $titletxt . "\n" .
"GAME URL:\t" . $titleurl . "\n" .
"LAYOUT:\t\t" . $layout . "\n\n" .
"LEGENDS:\n" . $legend . "\n\n" .
"COMMANDS:\n" . $command . "\n\n" .
"BINDINGS:\n" . $binding . "\n\n",
$headers
);
header()
函数将原始HTTP标头发送到客户端浏览器,但mail
函数与变量$headers
将标头发送到电子邮件。