Zisis Paparidis
Zisis Paparidis

Reputation: 37

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

I have a very simple html contact form that asks from the user a name, an email and then the message area. The problem is that when the user enters their name in greek characters (as the site is in greek language), the message never gets delivered. I tested it thoroughly and I found out that there is no problem if in the textarea there are greek characters, the problem appears only in the name field. The code for my contact form is this one:

<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>

As you can see, it calls an external php script, which after messing with for a whole day but without a positive result looks like this:

<?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 [email protected]";

    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 = "[email protected]";
    $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.";
  }
?>

I spent my whole day doing all sorts of experiments but as I am not a programmer, I failed to make it work. For any kind people that will respond with a possible solution, please remember, I am NOT a programmer.

Upvotes: 0

Views: 459

Answers (1)

Andrei Filonov
Andrei Filonov

Reputation: 834

I think you actually have a problem with the mail command call arguments - looks like you put your sender's info as your second argument while it should be subject line.

So, when I replaced your call with

mail($recipient, $subject, $email_content, $from, $options)

it worked just fine for me with UTF in the name field.

Upvotes: 0

Related Questions