PHP & International Characters - Web form jumbling text

I’m using a fairly simple php script to process a contact form. The web page the form is on is coded in UTF-8. The problem is when someone from another country that has special characters fills out the form it comes in al jumbed like this:
Bitte Rückmeldung bezüglich Pektine
I’m sure there’s some um lauds etc missing in there. How can I prevent this? I googled around but any solutions seemed to be geared toward PHP developers. I’m a noob so I need it dumbed down.

Thanks!!!

Here is my form processor code:

<?php




// ###########################################################################
// #### CONFIGURE FROM: ADDRESS ##############################################

// THIS AREA IS OPTIONAL. ONLY TOUCH THESE SETTINGS IF YOU KNOW WHAT YOU ARE
// DOING. PLEASE READ README.TXT FOR MORE INFORMATION.

// If you would like to specify the From: address of emails sent by DynaForm,
// enter it between the double quotes below. If you leave this blank, the
// server will assign the default email address.

$from_address = "contact@website.com";

// ###########################################################################
// ###########################################################################





// ###########################################################################
// #### ACTIVATE REQUIRED FIELDS? ############################################

// THIS AREA IS OPTIONAL. ONLY TOUCH THESE SETTINGS IF YOU KNOW WHAT YOU ARE
// DOING. PLEASE READ README.TXT FOR MORE INFORMATION.
//
// If you would like to make some fields of your form required, change "no" to
// "yes" below.

$required_on = "yes";

// If you have set $required_on to "yes" above, you can make fields required
// by beginning their name with "r_". For example, if you want to require
// a user to enter their name, use the following HTML:
//
// <input type='text' name='r_Name'>
//
// If a user fails to enter a required field, they will be taken to a page
// where a message such as "You have not completed all the required fields."
// will be displayed. Please specify the URL to this file below:

$required_errorpage = "../../contact-error.html";

// ###########################################################################
// ###########################################################################





// ###########################################################################
// #### OVERRIDE REQUIRED VARIABLES? #########################################

// THIS AREA IS OPTIONAL. ONLY TOUCH THESE SETTINGS IF YOU KNOW WHAT YOU ARE
// DOING. PLEASE READ README.TXT FOR MORE INFORMATION. NOTE: THIS WILL NOT
// AFFECT YOUR 'TURN ON REQUIRED FIELDS?' SECTION SETTINGS ABOVE.
//
// If you would like to override the three required variables in
// order to hide your email address, email subject, and thank you page
// URL from your email form, change "no" to "yes" below.

$override = "yes";

// If override is set to "yes", the hidden variables on your HTML
// email form named "rec_mailto", "rec_subject", and "rec_thanks" will be
// overridden and can therefore be removed from the form.

// If you have set override to "yes" above, you must specify new values for
// each of these variables below.

// Enter the email address(es) to send the email to.
$incoming_mailto = "HIDDEN";

// Enter the email subject.
$incoming_subject = "Contact Form submission from HIDDEN";

// Enter the thank you page URL.
$incoming_thanks = "../../contact-thanks.html";

// ###########################################################################
// ###########################################################################





// ###########################################################################
// #### BAN IP ADDRESSES? ####################################################

// THIS AREA IS OPTIONAL. ONLY TOUCH THESE SETTINGS IF YOU KNOW WHAT YOU ARE
// DOING. PLEASE READ README.TXT FOR MORE INFORMATION.
//
// If you would like to ban certain IP addresses from submitting your form,
// change "no" to "yes" below.

$ban_ip_on = "no";

// If you have set $ban_ip_on to "yes" above, please enter a list of the
// IP addresses you would like to ban, seperated only by commas.
// An example has been provided below:

$ban_ip_list = "111.222.33.55,11.33.777.99";

// ###########################################################################
// ###########################################################################





// ###########################################################################
// #### ACTIVATE DOMAIN SECURITY? ############################################
//
// THIS AREA IS OPTIONAL. ONLY TOUCH THESE SETTINGS IF YOU KNOW WHAT YOU ARE
// DOING. PLEASE READ README.TXT FOR MORE INFORMATION.
//
// This setting, when set to "yes" (default), will check to make sure other
// people are not submitting data to your dynaform.php file from their
// external domains. This means that if your domain name is "www.mysite.com",
// only forms on "www.mysite.com" will be able to use this dynaform.php.
// IF YOU ARE RECEIVING ERRORS SUCH AS "INVALID DOMAIN" FOR NO REASON, PLEASE
// CHANGE "yes" TO "no" BELOW.

$secure_domain_on = "no";

// ###########################################################################
// ###########################################################################




// ###########################################################################
// #### ACTIVATE AUTO-RESPONSE? ##############################################
//
// THIS AREA IS OPTIONAL. ONLY TOUCH THESE SETTINGS IF YOU KNOW WHAT YOU ARE
// DOING. PLEASE READ README.TXT FOR MORE INFORMATION.
//
// This setting, when set to "yes", will make DynaForm automatically reply to
// the user who submitted your form with an email message. If you would like
// to use this feature, change "no" to "yes" below.

$autorespond_on = "no";

// If you have set $autorespond_on to "yes" above, you must specify a subject,
// from-address, and message to include in the auto-response email.

// The following setting is the subject of the auto-response email:
$autorespond_subject = "Your Form Submission";

// The following setting is the from-address of the auto-respond email:
$autorespond_from = "youremail@yoursite.com";

// The following setting is the message of your auto-response email:
$autorespond_contents = "Your submission from our website has been received. Thank you!";

// DynaForm also needs to know how to retrieve the user's email address.
// You must specify the name of the field into which the user will enter
// their email address. For example, if your email form contains an input
// field like "<input type='text' name='Email'>" you would set the
// following setting to "Email".
$autorespond_mailto_field = "Email";

// ###########################################################################
// ###########################################################################











// MAKE SURE DYNAFORM IS NOT BEING LOADED FROM THE URL
if($_SERVER['REQUEST_METHOD'] == "GET") {
echo "
<html>
<head><title>Webligo PHP DynaForm is installed correctly.</title></head>
<body>
<font style='font-family: verdana, arial; font-size: 9pt;'>
<b>DynaForm is installed correctly.</b></font><br>
<font style='font-family: verdana, arial; font-size: 8pt;'>
DynaForm Easy PHP Form Mailer was created by <a href='http://www.webligo.com'>Webligo Developments</a>.
</font>
</body></html>
";
exit();
}

// SET VARIABLES
$incoming_fields = array_keys($_POST);
$incoming_values = array_values($_POST);

if($override == "no") {
$incoming_mailto = $_POST['rec_mailto'];
$incoming_subject = $_POST['rec_subject'];
$incoming_thanks = $_POST['rec_thanks'];
}

$incoming_mailto_cc = $_POST['opt_mailto_cc'];
$incoming_mailto_bcc = $_POST['opt_mailto_bcc'];
$form_url = $_POST[HTTP_REFERER];

// MAKE SURE DYNAFORM IS BEING RUN FROM THE RIGHT DOMAIN
if($secure_domain_on == "yes") {
$form_url_array = parse_url($form_url);
$form_domain = $form_url_array[host];
if($form_domain != $_SERVER[HTTP_HOST]) {
echo "<h2>DynaForm Error - Invalid Domain</h2>
You have accessed DynaForm from an external domain - this is not allowed.<br>
You may only submit forms to a DynaForm file that exists on the same domain name.<br>
If you believe to be receiving this message in error, please refer to your readme.txt file.
<br><br>";
$error = "yes";
}
}

// CHECK IF MAILTO IS SET
if($incoming_mailto == "") {
echo "<h2>DynaForm Error - Missing Field</h2>
Your form located at <a href='$form_url'>$form_url</a> does not work because you forgot to include
the required \\"<b>rec_mailto</b>\\" field within the form. This field specifies who the email will
be sent to.
<br><br>
This should look like:<br>
<input type=\\"hidden\\" name=\\"rec_mailto\\" value=\\"youremail@yoursite.com\\">
<br><br>
If you are still confused, please refer to the readme.txt for more information and examples.<br><br><br><br>
";
$error = "yes";
}

// CHECK IF SUBJECT IS SET
if($incoming_subject == "") {
echo "<h2>DynaForm Error - Missing Field</h2>
Your form located at <a href='$form_url'>$form_url</a> does not work because you forgot to include
the required \\"<b>rec_subject</b>\\" field within the form. This field specifies the subject of
the email that will be sent.
<br><br>
This should look like:<br>
<input type=\\"hidden\\" name=\\"rec_subject\\" value=\\"New DynaForm Email\\">
<br><br>
If you are still confused, please refer to the readme.txt for more information and examples.<br><br><br><br>
";
$error = "yes";
}

// CHECK IF THANKS IS SET
if($incoming_thanks == "") {
echo "<h2>DynaForm Error - Missing Field</h2>
Your form located at <a href='$form_url'>$form_url</a> does not work because you forgot to include
the required \\"<b>rec_thanks</b>\\" field within the form. This field specifies what page the user
will be taken to after they submit the form.
<br><br>
This should look like:<br>
<input type=\\"hidden\\" name=\\"rec_thanks\\" value=\\"thanks.html\\">
<br><br>
If you are still confused, please refer to the readme.txt for more information and examples.<br><br><br><br>
";
$error = "yes";
}

// CHECK IF IP ADDRESS IS BANNED
if($ban_ip_on == "yes") {

if(strstr($ban_ip_list, $_SERVER[REMOTE_ADDR])) {
echo "<h2>DynaForm Error - Banned IP</h2>
You cannot use this form because your IP address has been banned by the administrator.<br>
";
$error = "yes";
}
}


if($error == "yes") {
exit();
}

// SET EMAIL INTRODUCTION
$message = "This email was received from your Contact Form located at HIDDEN \
\
";

// LOAD EMAIL CONTENTS 
for ($i = 0; $i < count($incoming_fields); $i++) { 
if($incoming_fields[$i] != "rec_mailto") {
if($incoming_fields[$i] != "rec_subject") {
if($incoming_fields[$i] != "rec_thanks") {
if($incoming_fields[$i] != "opt_mailto_cc") {
if($incoming_fields[$i] != "opt_mailto_bcc") {

// CHECK FOR REQUIRED FIELDS IF ACTIVATED
if($required_on == "yes") {
$sub = substr($incoming_fields[$i], 0, 2);
if($sub == "r_") {
if($incoming_values[$i] == "" OR !isset($incoming_values[$i]) OR $incoming_values[$i] == " ") {
header("Location: $required_errorpage");
exit();
}}}

// ADD FIELD TO OUTGOING MESSAGE
$message .= "$incoming_fields[$i]:\
$incoming_values[$i]\
\
";
}}}}}}

// SET EMAIL FOOTER
$message .= "\
\
";

// CLEAR HEADERS
$headers = "";

// ADD FROM ADDRESS
if($from_address != "") {
$headers .= "From: $from_address\\r\
";
}

// CHECK FOR CC OR BCC
if($incoming_mailto_cc != "") {
$headers .= "Cc: $incoming_mailto_cc\\r\
";
}
if($incoming_mailto_bcc != "") {
$headers .= "Bcc: $incoming_mailto_bcc\\r\
";
}

// SEND EMAIL
mail($incoming_mailto, $incoming_subject, $message, $headers);

// SEND AUTO-RESPONSE IF ACTIVATED
if($autorespond_on == "yes") {
$autorespond_mailto = $_POST[$autorespond_mailto_field];
$autorespond_headers = "From: $autorespond_from";
mail($autorespond_mailto, $autorespond_subject, $autorespond_contents, $autorespond_headers);
}

// FORWARD TO THANK YOU PAGE
header("Location: $incoming_thanks"); 


?>

Is this just for emails? If it is, try forcing utf-8 headers into your mail function

ie:


$headers  = 'MIME-Version: 1.0' . "\\r\
";
$headers .= 'Content-type: text/html; charset=utf-8' . "\\r\
";

Thanks, I’ll try that and see if it works.

I added those lines and it worked for displaying international characters. But now the email has no line breaks in it and is just one long string of text. It used to break after each input field in the email. Do you know why this is happening or how I could add those breaks in?

Did you view the source and see what it looked like?

Those headers allow you to input HTML so adding <br /> shoulnd’t be a problem.

I’m not sure where to add the <br /> tags to this code. Or how. The email message builds itself automatically by what input fields were filled in. I don’t know where it builds the html. The emails also appear to be plain text.

// LOAD EMAIL CONTENTS 
for ($i = 0; $i < count($incoming_fields); $i++) { 
if($incoming_fields[$i] != "rec_mailto") {
if($incoming_fields[$i] != "rec_subject") {
if($incoming_fields[$i] != "rec_thanks") {
if($incoming_fields[$i] != "opt_mailto_cc") {
if($incoming_fields[$i] != "opt_mailto_bcc") {

// CHECK FOR REQUIRED FIELDS IF ACTIVATED
if($required_on == "yes") {
$sub = substr($incoming_fields[$i], 0, 2);
if($sub == "r_") {
if($incoming_values[$i] == "" OR !isset($incoming_values[$i]) OR $incoming_values[$i] == " ") {
header("Location: $required_errorpage");
exit();
}}}

// ADD FIELD TO OUTGOING MESSAGE
$message .= "$incoming_fields[$i]:\
$incoming_values[$i]\
\
";
}}}}}}

// SET EMAIL FOOTER
$message .= "\
\
";

// HEADERS
$headers  = 'MIME-Version: 1.0' . "\\r\
";
$headers .= 'Content-type: text/html; charset=utf-8' . "\\r\
";

// ADD FROM ADDRESS
if($from_address != "") {
$headers .= "From: $from_address\\r\
";
}

Try removing the \r from the headers variable


$headers  = 'MIME-Version: 1.0' . "\
";
$headers .= 'Content-type: text/html; charset=utf-8' . "\
"; 

What I meant by view the source is that when you see the output, right click on your window and click view source to see what the browser is really saying to you.

If you’re seeing a bunch of HTML tags that means that it’s not rendering the HTML for you properly.

I tried removing the /r. That didn’t work. Here is the View selection source in Firefox Gmail of the highlighted message body. (I auto filled the form with the web dev add-on. that’s why it has the same name in the field for each one.)

<div id=":1rd" class="ii gt">
This email was received from your Sample Request Form located at Cpkelco.com 

r_Name:
r_Name

r_Company:
r_Company

r_Address:
r_Address

Address_2:
Address_2

r_City:
r_City

State:
State

r_ZipPostalCode:
r_ZipPostalCode

r_Country:
United States

r_Email:
<a href="mailto:example@example.com" target="_blank">example@example.com</a>

r_Phone:
r_Phone

r_Application:
r_Application

r_Message:
r_Message á á á




</div>

The email looks like this:

This email was received from your Sample Request Form located at Cpkelco.com r_Name: r_Name r_Company: r_Company r_Address: r_Address Address_2: Address_2 r_City: r_City State: State r_ZipPostalCode: r_ZipPostalCode r_Country: United States r_Email: example@example.com r_Phone: r_Phone r_Application: r_Application r_Message: r_Message á á á

Yeah, that’s more of a problem than it should be. PHP 6.0 will be the first Unicode compliant version and it’s still in beta. Not only is it still in beta, I believe rewriting it to be Unicode compliant is the big piece that’s occupying a lot of the remaining work. It’s not simple to work around at the moment as you can see.

Is there a way to force those linebreaks after each input?