0

I am getting some data from a html form and sending them by email with PHP. The issue is that i am getting weird characters because the language that the user writes in the form is Czech.

For example the name Anežka becomes Anežka

This is the code i have for my php which being used for sending the email.

<?php 
if(isset($_POST['submit'])){
    $to_alex = "myemail@honeywell.com"; // this is your Email address
    $first_name = $_POST['fname'];
    $first_name=mb_convert_encoding($first_name, "UTF-8", "ISO-8859-1");
    $last_name = $_POST['lname'];
    $email = $_POST['email'];
    $skola = $_POST['vysoka_skola'];
    $obor = $_POST['obor'];
    $prace = $_POST['prace'];
    $phone_num=$_POST['phone_number'];
    $linkedin=$_POST['linkedin'];
    $oponenta=$_POST['oponenta'];
    $vedouciho=$_POST['vedouciho'];

    $files = $_FILES['myfile']['name'];
    $kategorie = $_POST['kategorie'];
    //echo gettype($files);
    $all_thesis_string="";
    $all_vedouciho_string="";
    for($index=0;$index<count($_FILES['myfile']['name']);$index++){
        $all_thesis_string.=$_FILES['myfile']['name'][$index].","; //create a string with all bachelor attachments of user
    }
    for($index=0;$index<count($_FILES['vedouciho_files']['name']);$index++){
        $all_vedouciho_string.=$_FILES['vedouciho_files']['name'][$index].","; //create a string with all vedouciho attachments of user
    }
    for($index=0;$index<count($_FILES['oponenta_files']['name']);$index++){
        $all_vedouciho_string.=$_FILES['oponenta_files']['name'][$index].","; //create a string with all oponenta attachments of user
    }

    $subject = "Form submission of ".$first_name." ".$last_name;
    $message = "User Details: \n\n Jméno: $first_name \n Příjmení: $last_name \n Email: $email \n Telefon: $phone_num \n Linkedin: $linkedin \n Vysoká škola: $skola \n Studijní obor: $obor \n Odkaz na bakalářskou práci: $prace \n Kategorie: $kategorie \n Posudek oponenta: \n $oponenta \n Posudek vedouciho: \n $vedouciho \n Bakalářská práce: $all_thesis_string \n Vedouciho files: $all_vedouciho_string";
    $headers = "From:" . $first_name;
    mail($to_alex,$subject,$message,$headers);

    echo '<span style="color:#AFA;text-align:center;"><strong>"Přihláška úspěšně odeslána. Děkuji "</strong></span>';
    //include 'upload.php';
    // You can also use header('Location: thank_you.php'); to redirect to another page. 
    }
?>

I have tried mb_convert_encoding in my last attempt but still i have the issue.

Alex
  • 1,816
  • 5
  • 23
  • 39
  • 2
    ISO-8859-1 cannot store the Czech alphabet. Do you really need to use any other encoding apart from UTF-8? – Álvaro González Jul 26 '21 at 11:48
  • @ÁlvaroGonzález I don't mind using any encoding . I just need it to work! I supposed utf8 is the best for this no? – Alex Jul 26 '21 at 11:51
  • Then UTF-8 is all you need. You don't need to convert between encodings, you need to review your application flow for places where you either set the wrong encoding or you fail to set any (thus it defaults to the wrong one). This gets asked often and there's a good question already with many hints, let me link to that. – Álvaro González Jul 26 '21 at 14:20

1 Answers1

0

You need to tackle possible issues on both input and output stages of your program:

  1. make sure, the browser knows, that you want UTF-8: It needs the tag <meta charset="utf-8"> somewhere in the <head> of your HTML. Otherwise it is possible, that it falls back to some legacy encoding when sending form data back to you.

  2. make sure, e-mail clients know, that you send UTF-8 to them. They, too, will fall back to legacy encodings without explicitly telling them. Try adding these headers to the mail:

    Mime-Version: 1.0
    Content-Type: text/plain; charset=utf-8
    Content-Transfer-Encoding: quoted-printable
    
  3. If you want UTF-8 in the subject and/or To fields, you need a separate way of quoting. I suggest you take a look at the excellent PHPMailer package that will handle all that for you.

Boldewyn
  • 81,211
  • 44
  • 156
  • 212