Jump to content

Contact form character encoding problem


janlonden
 Share

Recommended Posts

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

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]");

  • Like 1
Link to comment
Share on other sites

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 :D

How would i use this code for $message?

Thanks!

Link to comment
Share on other sites

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!

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...