I'm having an issue with accented characters sent from my simple HTML contact form. When I recieve an email, which contains some accented characters(ľ,š,č,ť,ž,ý,á,í,é,ú,ä,ô,...) it's just unreadable. I see it's pretty common issue but I still was not able to implement solutions that have already been answered to my PHP.
I set up the meta tag for utf-8 already into <head>
of HTML and I also tried to input header into PHP file, but honestly I'm not realy sure where exactly should he be placed.
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
Here is code of my form:
<form name="contactForm" method="POST" action="mail_sender.php">
<div class="row">
<!--NAME-->
<div class="control-group col-md-6 col-sm-6">
<label class="control-label" for="inputName">MENO<sup>*</sup></label>
<div class="controls">
<input class="form-control" name="name" type="text" placeholder="meno">
</div>
</div>
</div>
<div class="row">
<!--EMAIL-->
<div class="control-group col-md-6 col-sm-6">
<label class="control-label" for="inputEmail">EMAIL<sup>*</sup></label>
<div class="controls">
<input name="email" type="text" class="form-control col-md-4 col-sm-4" placeholder="email">
</div>
</div>
</div>
<div id='contactForm_errorloc'></div>
<div class="row">
<!--PHONE-->
<div class="control-group col-md-6 col-sm-6">
<label class="control-label" for="inputEmail">TELEFÓN<sup>*</sup></label>
<div class="controls">
<input name="phone" type="text" class="form-control col-md-4 col-sm-4" placeholder="telefónne číslo">
</div>
</div>
</div>
<div class="row">
<!--MESSAGE-->
<div class="control-group col-md-12 col-sm-12">
<label for="textarea" class="control-label">SPRÁVA<sup>*</sup></label>
<div class="controls">
<textarea class="form-control" name="message" rows="4" id="textarea"></textarea>
</div>
</div>
</div>
<div id='contactForm_errorloc'></div>
<div class="control-group form-button-offset">
<input style="margin-top: 10px;" type="submit" class="btn btn-default" value="Poslať správu" />
</div>
</form>
Here's code of my PHP file:
<?php
$errors = '';
$myemail = 'email@domain.com';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Phone: $phone \n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
</html>
I tried many things to make it work without success. Thank you for your time, I'll appreciate your answers.