电子邮件表单弄乱了UTF-8文本

时间:2019-03-11 00:05:17

标签: php html5 forms email

我的网站上有一个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
    (
        "blahblah@blah.com",
        "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"
    );

1 个答案:

答案 0 :(得分:1)

例如,请尝试将 utf-8 字符集标头添加到您的邮件功能中

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

$headers .= 'From: <example@example.com>' . "\r\n";
$headers .= 'Cc: example@example.com' . "\r\n";

 mail
    (
        "blahblah@blah.com",
        "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的{​​{1}}函数将标头发送到电子邮件。