在名称字段

时间:2016-08-22 17:24:07

标签: php contact-form non-latin

我有一个非常简单的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.";
  }
?>

我花了一整天时间做各种各样的实验,但由于我不是程序员,所以我没能让它成功。对于那些会回答可能解决方案的人,请记住,我不是程序员。

1 个答案:

答案 0 :(得分:0)

我认为你实际上有一个邮件命令调用参数的问题 - 看起来你把发件人的信息作为你的第二个参数,而它应该是主题行。

所以,当我用

替换你的电话时
mail($recipient, $subject, $email_content, $from, $options)

在名称字段中使用UTF对我来说效果很好。