0
votes

I have small problem with my php contact form code. I have my site using diacritics which does work perfectly. But my contact form input after submitting looks like this "ÄÅ¡Å" after using any [0-9] keys for diacritics.

PHP file:

<?php


$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$service = $_POST['service'];      
$message = $_POST['message'];      

$from = $name; 
$to = 'info@email.com'; 
$subject = "$name - $service";

$body = "Jméno zákazníka: $name\nE-Mail zákazníka: $email\nTelefonní číslo: $phone\nservice: $service\nZpráva: $message ";

if (mail ($to, $subject, $body, $from, $headers)) {
    $headers = array("Content-Type: text/html; charset=UTF-8");
} else {
    echo "Failed";
}


?>

Email Output after submitting the contact form:

First Last Name

Jméno zákazníka: Name
E-Mail zákazníka: email
Telefonní císlo: phone number
service: havárii
Zpráva: ÄÅ¡Å

the "headers" as name, email, phone, service, message is written with diacritics BUT then the input for example here: Zpráva: ÄÅ¡Å is not supported for some reason.

Also the html file include

<meta charset="utf-8">

UPDATE 1

Have tried to put $headers = array("Content-Type: text/html; charset=UTF-8"); before the contact form is submitted but no change were made. Still doesnt support any of these characters: ě š č ř ž ý á í é

MORE INFO

Adding also the html part where the problem is mostly visible

<div class="col-lg-12">
<textarea name="message" id="" cols="30" rows="10" placeholder="Zpráva"> 
</textarea>
</div>
1
Does your submitting form also have UTF-8 character set specified?Philip
@Philip I have my contact form in html file where is the <meta charset="utf-8"> already set in body.Michael Florian
@Ingus No, the form is focused on header with I have already set.Michael Florian
I think there some info is missing. ..Ingus
" input after submitting" ...where are you seeing it after submitting exactly? Your PHP does not appear to re-output any of the submitted data. Are you talking about what you see in the email?ADyson

1 Answers

1
votes

You need to include your headers as the 4th param of mail():

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$service = $_POST['service'];      
$message = $_POST['message'];      

$from = $name; 
$to = 'info@email.com'; 
$subject = "$name - $service";

$body = "Jméno zákazníka: $name\nE-Mail zákazníka: $email\nTelefonní číslo: $phone\nservice: $service\nZpráva: $message ";

$headers = [
    'Content-Type' => 'text/plain; charset=UTF-8',
    'From' => $from,
];

if (mail($to, $subject, $body, $headers)) {
    echo "Sent";
} else {
    echo "Failed";
}

Note that this is for plain text email, not for HTML email.

From should be formatted as such: Name <email@example.com>, so if you don’t have information, then do not include it.