PHP 来自表单的电子邮件争夺西里尔字符

PHP emails from form scramble cyrillic characters

这就是全部内容。由于我今天使用的是 PHP,因此我面临着这个问题。 我为我工作的 ISP 公司建立了一个表格,它通过电子邮件发送表格结果。客户填写此表格以请求我们提供新服务。由于公司是外国的(保加利亚语),99%的人都会用西里尔文填写,所以我自己填写测试了一下,收到了这封邮件。


发件人:ÐÑд

Phone 人数:56

选定的服务:

互联网:100 ||电视:舒适

留言:

ЪЕÐÐÐÐÐÈ


所以我做了一些研究,在 Whosebug 中查看了一些关于此的其他帖子,但 none 有效...大多数建议添加 $header .= "\nContent-type: text/plain; charset=\"utf-8\"";

一个问题是我不确定如何实施这个解决方案,第二个问题是它对提出问题的人不起作用,甚至在我审查的其中一个问题中也不起作用。

这是 PHP 代码和 HTML 代码,希望你能给我一个修复它的确切方法,因为我几乎不知道 PHP 中的语法。

<!DOCTYPE html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Online Request</title>
<meta content="php, contact, form, thinking" name="keywords">
<meta content="Contact us and let us know if we can help you out further." name="description">

<style>
input, textarea {
    color: #fff;
    padding: 5px;
    margin: 10px;
    font-family: Cambria, Cochin, serif;
    font-size: medium;
    font-weight: bold;
    outline: none;
}
p {
    font-family: Cambria, Cochin, serif;
    font-size: large;
    margin-bottom: -5px;
}
input[type=text], textarea {
    width: 350px;
    background-color: #000;
    border: 1px solid #000;
}
input[type=submit] {
    width: 100px;
    background-color: #710000;
    border: 1px solid #000;
    font-size: large;
    color: #FFFFFF;
}
input[type=submit]:hover {
    background-color: #990000;
    cursor: pointer;
}
input[type=submit]:active {
    background-color: #4A6F00;
}

h1 {
  text-size: 26px;
  font-family: Arial;
  text-shadow:
  -1px -1px 0 #000,
  1px -1px 0 #000,
  -1px 1px 0 #000,
  1px 1px 0 #000;
  color: #ff0000;}
body {
    padding: 10px;
    background-color: #F4F4F4;
}
.checkbox {
  font-family: Cambria, Cochin, serif;
  font-size: large;
  margin-bottom: -5px;
}
</style>

</head>

<body>
    <h1>Online Request</h1>
    <form action="emailer.php" method="POST" accept-charset="UTF-8">
        <div>
            <p>Имена</p>
            <input name="name" type="text"> <br>
        </div>
        <div>
            <p>Телефон за връзка</p>
            <input type="text" name="number" min="10" max="10">
            <br>
        </div>
        <div>
            <p> Вид услуга - Internet </p> <br/>
            <input name="servicetypeINT" type="radio" value="30"><span class="checkbox"> Internet Value - 30Mpbs </span> <br/>
            <input name="servicetypeINT" type="radio" value="60"><span class="checkbox"> Internet Mania - 60 Mbps </span><br/>
            <input name="servicetypeINT" type="radio" value="100"><span class="checkbox"> Internet Extreme - 100Mbps</span> <br/>
            <input name="servicetypeINT" type="radio" value="150"><span class="checkbox"> Internet Pro - 150Mpbs</span><br/>
        </div>
        <hr/>
        <div>
            <p>Вид услуга - Телевизия</p> <br/>
            <input name="servicetypeTV" type="radio" value="anal"><span class="checkbox"> Аналогова телевизия - 50 канала </span> <br/>
            <input name="servicetypeTV" type="radio" value="start"><span class="checkbox"> Start TV - 40+ Цифрови канала </span><br/>
            <input name="servicetypeTV" type="radio" value="comfort"><span class="checkbox"> Confort TV - 160+ Цифрови канала</span> <br/>
            <input name="servicetypeTV" type="radio" value="mania"><span class="checkbox"> Mania TV - 160+ Цифрови / 20+ HD канала</span><br/>
        </div>
            <div>
            <p>Comment</p>
            <textarea cols="30" name="comment" rows="9"></textarea>
            <br> </div>
        <div>
        <input name="submit" type="submit" value="Изпрати"> </div>
    </form>
</body>

</html>


<?php

if(isset($_POST['submit'])) {
$to = "denislav@svishtov.net";
$subject = "New Internet and/or TV request";

// data the visitor provided
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$phone_field = filter_var($_POST['number']);
$selectedINT_field = filter_var($_POST['servicetypeINT']);
$selectedTV_field = filter_var($_POST['servicetypeTV']);
$comment = filter_var($_POST['comment'], FILTER_SANITIZE_STRING);


//constructing the message
$body = "
From: $name_field\n\n
Phone number: $phone_field\n\n
Selected Services:\n\nInternet: $selectedINT_field || TV: $selectedTV_field \n\n
Message:\n\n $comment ";

// ...and away we go!
mail($to, $subject, $body);

// redirect to confirmation
header('Location: confirmation.html');
} else {
// handle the error somehow
echo "Грешка в попълването на формата";
}
?>

另一个有趣的事实是,当我从我的本地机器打开文件时(因为如果我从在线托管的机器上尝试,我会收到错误回显)php 代码中的西里尔文本本身是就像电子邮件结果一样显示... 但是当我从主机打开它时,通过直接寻址它我得到了正确的错误消息......保加利亚语......没有任何乱码。

PHP

中的代码行
header('Content-Type: text/html; charset=utf-8');

似乎转换了 header 消息。我尝试解决 $name_field('Content-Type: text/html; charset=utf-8');

但结果是 空白,php 没有正常运行,我猜是因为我给了它一个错误的或无法操作的带有该地址的代码行共 $name_field.

问题是,我该如何正确解决它,我什至用这种方法走上了正确的轨道?

已修复 在 php 代码

的开头添加了 $headers 内容类型
<?php
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text; charset=utf-8' . "\r\n";

并在 MAIL 函数中包含 $headers

mail($to, $subject, $body, $headers);

我花了四个小时,但一开始就是这样。 无论如何,感谢您的帮助,或缺乏这样的帮助。祝你有美好的一天,希望这对某人有所帮助