我有一个非常简单的 html 联系表单,它向用户询问姓名、电子邮件,然后是消息区域。问题是当用户以希腊字符输入他们的名字时(因为该站点是希腊语言),消息永远不会被传递。我彻底测试了一下,发现如果textarea中有希腊字符没有问题,问题只出现在名称字段中。我的联系表格的代码是这个:
<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>\r\nReturn-path: $email";
$subject = "New contact from $name - my-website.gr";
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
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.";
}
?>
我花了一整天的时间做各种各样的实验,但由于我不是程序员,我没能成功。对于任何会以可能的解决方案做出回应的人,请记住,我不是程序员。