在名称字段中使用希腊字符时,联系表单不会发送邮件

Contact form doesn't send the mail when greek characters are used in the name field

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

我整天都在做各种实验,但由于我不是程序员,所以没能成功。对于任何愿意提供可能解决方案的人,请记住,我不是程序员。

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

所以,当我用

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

我在名称字段中使用 UTF 时效果很好。