janlonden Posted September 7, 2013 Share Posted September 7, 2013 I've encountered a problem with my contact form. Characters such as å, ä and ö in mails sent from the form are displayed as Ã¥, À, and ö. I'm sure it has something to do with character encoding(utf-8, iso-8859-1). I've googled around for a solution but couldn't find an answer Any ideas how to fix this? Here's my contact form code: // set this to the email address you want to send to (or pull from a PW field) $emailTo = ''; // or if not set, we'll just email the default superuser if(empty($emailTo)) $emailTo = $users->get($config->superUserPageID)->email; // set and sanitize our form field values $form = array( 'namn' => $sanitizer->text($input->post->namn), 'epost' => $sanitizer->email($input->post->epost), 'meddelande' => $sanitizer->textarea($input->post->meddelande), ); // initialize runtime vars $sent = false; $error = ''; // check if the form was submitted if($input->post->submit) { // determine if any fields were ommitted or didn't validate foreach($form as $key => $value) { if(empty($value)) $error = "<p class=\"error\">Kontrollera att du har fyllt i alla fält.</p>"; } // if no errors, email the form results if(!$error) { $subject = "Kontakt formulär"; $message = ''; foreach($form as $key => $value) $message .= "$key: $value\n"; mail($emailTo, $subject, $message, "From: $form[epost]"); $sent = true; } } if($sent) { echo "<p class=\"success\">Tack! Ditt meddelande har skickats.</p>"; // or pull from a PW field } else { // encode values for placement in markup foreach($form as $key => $value) { $form[$key] = htmlentities($value, ENT_QUOTES, "UTF-8"); } echo $error; ?> <article role="complementary"> <form action="./" method="post"> <p> <label for="namn">Namn</label> <input type="name" id="namn" name="namn" aria-required="true" value="<?php echo $form[namn] ?>" required> </p> <p> <label for="epost">E-post</label> <input type="email" id="epost" name="epost" placeholder="du@domän.se" aria-required="true" value="<?php echo $form[epost] ?>" required> </p> <p> <label for="meddelande">Meddelande</label> <textarea id="meddelande" name="meddelande" aria-required="true" required><?php echo $form[meddelande] ?></textarea> </p> <input type="submit" id="submit" name="submit" value="submit"> </form> </article> <?php } ?> Link to comment Share on other sites More sharing options...
janlonden Posted September 7, 2013 Author Share Posted September 7, 2013 I tried changing utf-8 from this: $form[$key] = htmlentities($value, ENT_QUOTES, "UTF-8"); to iso-8859-1. But it didn't help. Link to comment Share on other sites More sharing options...
sakkoulas Posted September 7, 2013 Share Posted September 7, 2013 hi janlonden i had a same problem once with greek language and i found a solution in stackoverflow. i do not know if this will help you but you can give it a try. so instead of mail($emailTo, $subject, $message, "From: $form[epost]"); try this mail($emailTo, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, "From: $form[epost]"); 1 Link to comment Share on other sites More sharing options...
janlonden Posted September 7, 2013 Author Share Posted September 7, 2013 Partial success sakkoulas! The subject field is now showing åäö correctly but the problem remains in the message textarea. I've tried to move your code around, putting $message here and there without success hehe How would i use this code for $message? Thanks! Link to comment Share on other sites More sharing options...
sakkoulas Posted September 7, 2013 Share Posted September 7, 2013 my problem was solved only with the abobe code take a look at this snippet at the end of the page, in his mail $header he adds .'Content-Type: text/plain; charset=utf-8'."\r\n"; see the link below http://ncona.com/2011/06/using-utf-8-characters-on-an-e-mail-subject/ 2 Link to comment Share on other sites More sharing options...
janlonden Posted September 7, 2013 Author Share Posted September 7, 2013 You're awesome! This worked: if(!$error) { $subject = "Kontakt formulär"; $message = ''; $headers = 'From:'.$form[epost]."\r\n".'Content-Type: text/plain; charset=utf-8'."\r\n"; foreach($form as $key => $value) $message .= "$key: $value\n"; mail($emailTo, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $headers); $sent = true; } Thank you! 1 Link to comment Share on other sites More sharing options...
sakkoulas Posted September 7, 2013 Share Posted September 7, 2013 I'm glad I was able to help Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now