0

Hello guyz and thank you in advance. I have made an email form using php to send an email through a form from my website

Although the form works fine and the emails are sent I have a problem when I sent a message in my native language (Greek). If I write the message in English everything is fine and the email is view-able. If I send a message in Greek then the email is sent , but looks like this :

³Î¾ÏƒÎ»Î´Î³Î·ÎºÎ»ÏƒÎ´ ασλαηκσλκαςξδςακσ³Î¾ÏƒÎ»Î´Î³Î·ÎºÎ»ÏƒÎ´ ασλαηκσλκαςξδςακσ

Here is the code I use in my php form :

<?php

if(isset($_POST['email'])) {

    $email_to = "info@something.gr";

    $email_subject = "Email Form - WebSite";


    function died($error) {


        echo "We are very sorry, but there were error(s) found with the form you submitted. ";

        echo "These errors appear below.<br /><br />";

        echo $error."<br /><br />";

        echo "Please go back and fix these errors.<br /><br />";

        die();

    }

    $first_name = $_POST['name']; // required

    $email_from = $_POST['email']; // required

    $comments = $_POST['message']; // required

    $email_message = "Form details below.\n\n";

    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }    

    $email_message .= "First Name: ".clean_string($first_name)."\n";

    $email_message .= "Email: ".clean_string($email_from)."\n";

    $email_message .= "Message: ".clean_string($comments)."\n";


$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  

?>



<!-- include your own success html here -->


<center>
Thank you for contacting us. We will be in touch with you very soon.</br>
You will be redirected to our homepage in 5 seconds </center>
 <script type="text/javascript">
setTimeout("window.location='/'",5000);
</script>

<?php

}

?>
Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87

1 Answers1

1

The problem is with character encoding. By default, PHP E-mails are sent with ISO-8859-1. You'll need to swap to UTF-8 for languages like Greek :)

Fortunately, you can set this in the headers:

$headers = "Content-Type: text/html; charset=UTF-8";

Combined with your existing headers:

$headers = 'From: ' . $email_from . "\r\n" . 'Reply-To: ' . $email_from . "\r\n" . 'Content-Type: text/html; charset=UTF-8' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

Hope this helps!

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
  • It is probably very helpful and a probable solution but I dont know how to use this piece of code . – Symeon Laftsopoulos Dec 21 '16 at 06:09
  • Replace all of your code between $headers (inclusive) and @mail (exclusive) with the second block of code that I have written, try sending an E-mail again, and it should work :) – Obsidian Age Dec 21 '16 at 06:18
  • Tried the revised and now the Email is not sent at all . I get a success message on sending the form and the email is not sent. – Symeon Laftsopoulos Dec 21 '16 at 06:24
  • Glad to hear that it worked :) I was just typing out asking for a possible update to your code sample so that I can help further, but that won't be necessary ;) – Obsidian Age Dec 21 '16 at 06:36