Log in

View Full Version : UTF8 on my contact form



jonybigude
06-19-2013, 03:43 PM
Hi!

I have been searching on the web for solutions but for some reason it seems that I can't implement them. I am a rookie in PHP and perhaps the mistake has been under my nose all this time but I did not manage to see it...

Here goes my contact form:

<form method="post" action="kontakt.php">

<label>Namn *</label>
<input name="name" placeholder="Type Here">

<label>Telefon *</label>
<input name="telef" placeholder="Type Here">

<label>E-post *</label>
<input name="email" type="email" placeholder="Type Here">

<label>Meddelandet</label>
<textarea name="message" placeholder="Type Here"></textarea>

<label>*Hur mycket är 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">

<input id="submit" name="submit" type="submit" value="Submit">


<?php
$name = $_POST['name'];
$telef = $_POST['telef'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Lounge Terrasse';
$to = 'kontakt@lfsolutions.se';
$subject = 'Lounge Terrasse - Ny meddelande';
$human = $_POST['human'];

$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "From: $from <$email> ". PHP_EOL;
$headers .= "Content-type: text/html;charset=UTF-8 ". PHP_EOL;


$body = "From: $name\n Telefon: $telef\n E-Mail: $email\n Message:\n $message";

if ($_POST['submit']) {
if ($name != '' && $email != '' && $telef != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all required fields!!</p>';
}
}
?>

</form>

So... any suggestions in order to implement correctly the utf8, since the e-mails are still coming with weird characters?

Thanks in advance!!

traq
06-19-2013, 08:06 PM
This will depend on two factors:

1) the charset your webpage is being served with (check the HTTP headers; the <meta> is usually not sufficient on its own)

2) the charset your email is being sent with (check the email headers).

The form itself, strictly speaking, has nothing to do with it.

jonybigude
06-20-2013, 05:38 AM
Well, this is my header:


<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

And as you say, it does not help. As for the e-mail headers, isn't it what I have?


$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "From: $from <$email> ". PHP_EOL;
$headers .= "Content-type: text/html;charset=UTF-8 ". PHP_EOL;

traq
06-20-2013, 07:52 AM
Well, this is my header:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
check the actual HTTP header. If your server sends an HTTP header by default (virtually all do), browsers almost always take it as authoritative and ignore the <meta> element.
Most web browsers allow you to view the actual headers sent by the server.

It is best to set it in your Apache config (or .htaccess file), but you can also set it deliberately in your PHP script:
header('Content-Type: text/html; charset=utf-8');


And as you say, it does not help. As for the e-mail headers, isn't it what I have?
Double-check that they're actually getting sent that way (most email clients will have a "view headers" option).

jonybigude
06-20-2013, 09:49 AM
In the end of the day I manage to solve it like this:


<?php
$name = $_POST['name'];
$telef = $_POST['telef'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Lounge Terrasse: Ny meddelande';
$to = 'kontakt@lfsolutions.se';
$subject = 'Ny meddelande';
$human = $_POST['human'];


$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "From: $from <$email> ". PHP_EOL;
$headers .= "Content-type: text/html;charset=UTF-8 ". PHP_EOL;

$name = str_replace( '[at]','@', $name);
$message = str_replace( '[at]','@', $message);

$body = "<strong>Från:</strong> $name<br> <strong>Telefon:</strong> $telef<br> <strong>E-post:</strong> $email<br><br> <strong>Meddelandet:</strong><br> $message";


if ($_POST['submit']) {
if ($name != '' && $message != '' && $telef != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p style="background:#ff0000; line-height:30px; vertical-align:middle; color:#FFF; font-weight:bold;">Ditt meddelande har skickats!</p>';
} else {
echo '<p style="background:#ff0000; line-height:30px; vertical-align:middle; color:#FFF; font-weight:bold;">Någonting gick fel. Var vänlig och försök igen!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p style="background:#ff0000; line-height:30px; vertical-align:middle; color:#FFF; font-weight:bold;">Du har gett fel svar på Anti-Spam frågan!</p>';
}
} else {
echo '<p style="background:#ff0000; line-height:30px; vertical-align:middle; color:#FFF; font-weight:bold;">Du behöver att fylla alla fält i formuläret!</p>';
}
}

?>

Thanks for all the help traq! Much appreciated! :)