0

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.

dvglsk
  • 37
  • 1
  • 6
  • It's unreadable in your mail client ? – frz3993 Jul 29 '15 at 19:09
  • Yes, but it's not on the client side im pretty sure, since its not readable on any platform(mobile, etc.). – dvglsk Jul 29 '15 at 19:11
  • Try adding `"Content-Type: text/html; charset=UTF-8"` to your mail header. And use `\r\n` instead of `\n`. Change `text/html` to `text/plain` if you are not sending html mail. – frz3993 Jul 29 '15 at 19:15
  • @frz3993 already tried to add something like this: header('Content-Type: text/plain; charset=utf-8'); but it wasn't working...any idea where exactly to put it? – dvglsk Jul 29 '15 at 19:23
  • Your mail header, the `$headers` is what I'm talking about. `$headers .= "Content-Type: text/html; charset=UTF-8";` The `header()` function sends raw http header. Use `\r\n` to separate the headers. – frz3993 Jul 29 '15 at 19:26
  • @fzr3993 I tried both earlier today, but without much success. – dvglsk Jul 29 '15 at 19:30
  • @chris85 I dont think it's a duplicate because like I said i tried to implement quite a few solutions into this and I couldnt make it work so I'm more looking for something like piece of code that will make this exact form work. PHP is not my strongest side :). – dvglsk Jul 29 '15 at 19:37
  • `$headers = "From: $myemail\r\n";`,`$headers .= "Reply-To: $email_address\r\n";`,`$headers .= "Content-Type: text/html; charset=UTF-8\r\n";` Like this? – frz3993 Jul 29 '15 at 19:50
  • At what point at the accented characters lost. On form submission? when sending the email? when reading the email? – chris85 Jul 29 '15 at 20:05

0 Answers0