我有一个非常简单的html联系人表单,从用户询问姓名,电子邮件,然后消息区域。问题是,当用户用希腊字符输入他们的名字时(因为站点是希腊语),消息永远不会被传递。我对它进行了彻底的测试,我发现如果在文本区有希腊字符没有问题,问题只出现在名称字段。我的联系人表单的代码如下:
<form id="contact" method="post" action="mailer-backup.php" enctype="multipart/form-data" accept-charset="UTF-8">
<input type="text" id="name" name="name" required placeholder="Όνομα">
<input type="email" id="email" name="email" required placeholder="Email">
<textarea id="message" name="message" required placeholder="Μήνυμα"></textarea>
<button id="submit" type="submit">Αποστολή</button>
</form>
正如您所看到的,它调用了一个外部php脚本,在折腾了一整天之后,它看起来像这样:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("r","n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
$options="-f contact@my-website.gr";
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
http_response_code(400);
echo "All fields are required, please fill <a href="">the form</a> again.";
exit;
}
$recipient = "contact@my-website.gr";
$name = '=?utf-8?b?' . base64_encode($_POST['name']) . '?=';
$from="From: $name<$email>rnReturn-path: $email";
$subject = "New contact from $name - my-website.gr";
$email_content = "Name: $namen";
$email_content .= "Email: $emailnn";
$email_content .= "Message:n$messagen";
if (mail($recipient, '=?UTF-8?B?'.base64_encode($from).'?=', $subject, $email_content, $from, $options)) {
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
http_response_code(500);
echo "Tragic! Something went wrong and we couldn't send your message.";
}
} else {
echo "There was a problem with your submission, please try again.";
}
?>
我花了一整天的时间做各种各样的实验,但因为我不是程序员,我没能让它工作。对于任何一种人,将回应一个可能的解决方案,请记住,我不是一个程序员。
我认为你实际上有一个邮件命令调用参数的问题-看起来你把你的发件人的信息作为你的第二个参数,而它应该是主题行
所以,当我把你的电话换成
mail($recipient, $subject, $email_content, $from, $options)
在名称字段中使用UTF对我来说工作得很好。