Jump to content

Form does not accept characters like é etc.


kimikai

Recommended Posts

I have a question,

 

I have a fully working PHP mail form, but I can't seem to find one problem.

I thought it had something to do with the data cleansing like trim, htmlspecialcharacters and stripslashes, but unfortunately that wasn't.

 

My form has to be able to process characters like é è á ó etc.

Just that now when you fill those characters in it shows some weird code in the mail.

(é becomes é in the mail) and this is highly inconvenient.

 

Could any1 tell me how I can fix this?

this is the PHP code for my form:
 

<?php
    if(isset($_POST['submit']))
    {
		function test_input($data) {
			$data = trim($data);
			$data = stripslashes($data);
			$data = htmlspecialchars($data);
  			return $data;		
		}
		$error = "";
		
		//Keep Values
		$Papillon_checked = (isset($_POST['ras']) && $_POST['ras'] == 'Papillon') ? 'checked' : '';
		$Phalene_checked = (isset($_POST['ras']) && $_POST['ras'] == 'Phalene') ? 'checked' : '';
		$Babyklasse_checked = (isset($_POST['klasse']) && $_POST['klasse'] == 'Babyklasse') ? 'checked' : ''; 
		$Puppyklasse_checked = (isset($_POST['klasse']) && $_POST['klasse'] == 'Puppyklasse') ? 'checked' : ''; 
		$Jeugdklasse_checked = (isset($_POST['klasse']) && $_POST['klasse'] == 'Jeugdklasse') ? 'checked' : ''; 
		$Tussenklasse_checked = (isset($_POST['klasse']) && $_POST['klasse'] == 'Tussenklasse') ? 'checked' : ''; 
		$Openklasse_checked = (isset($_POST['klasse']) && $_POST['klasse'] == 'Openklasse') ? 'checked' : ''; 
		$Kampioensklasse_checked = (isset($_POST['klasse']) && $_POST['klasse'] == 'Kampioensklasse') ? 'checked' : ''; 
		$Fokkersklasse_checked = (isset($_POST['klasse']) && $_POST['klasse'] == 'Fokkersklasse') ? 'checked' : '';
		$Veteranenklasse_checked = (isset($_POST['klasse']) && $_POST['klasse'] == 'Veteranenklasse') ? 'checked' : ''; 
		
		//Validate form fields
	if (!empty($_POST['ras'])) {
		$ras = $_POST['ras'];
		} else {
		$error .= "- Klik het ras van uw hond aan. <br />";}
		
	if (!empty($_POST['kleur'])) {
		$kleur = test_input($_POST['kleur']);
		} else {
		$error .= "- Vul de kleur van uw hond in. <br />";}
		
	if (!empty($_POST['geslacht'])) {
		$geslacht = test_input($_POST['geslacht']);
		} else {
		$error .= "- Vul het geslacht van uw hond in. <br />";}
		
	if (!empty($_POST['naamhond'])) {
		$naamhond = test_input($_POST['naamhond']);
		} else {
		$error .= "- Vul de naam van uw hond in. <br />";}
		
	if (!empty($_POST['stamboom'])) {
		$stamboom = test_input($_POST['stamboom']);
		} else {
		$error .= "- Vul het stamboomnummer van uw hond in. <br />";}

	if (!empty($_POST['geboorte'])) {
		$geboorte = test_input($_POST['geboorte']);
		} else {
		$error .= "- Vul de geboortedatum van uw hond in. <br />";}
		
	if (!empty($_POST['klasse'])) {
		$klasse = $_POST['klasse'];
		} else {
		$error .= "- Klik de gewenste klasse aan. <br />";
		}
		
	if (!empty($_POST['fokker'])) {
		$fokker = test_input($_POST['fokker']);
		} else {
		$error .= "- Vul de naam van de fokker in. <br />";}
		
	if (!empty($_POST['vader'])) {
		$vader = test_input($_POST['vader']);
		} else {
		$error .= "- Vul de naam van de vaderhond in. <br />";}
		
	if (!empty($_POST['moeder'])) {
		$moeder = test_input($_POST['moeder']);
		} else {
		$error .= "- Vul de naam van de moederhond in. <br />";}
		
	if (!empty($_POST['initialen'])) {
		$initialen = test_input($_POST['initialen']);
		} else {
		$error .= "- Vul uw initialen in. <br />";}
		
	if (!empty($_POST['eigachternaam'])) {
		$eigachternaam = test_input($_POST['eigachternaam']);
		} else {
		$error .= "- Vul uw achternaam in. <br />";}
		
	if (!empty($_POST['minitialen'])) {
		$minitialen = test_input($_POST['minitialen']);}
	
	if (!empty($_POST['meigachternaam'])) {
		$meigachternaam = test_input($_POST['meigachternaam']);}
		
	if (!empty($_POST['straat'])) {
		$straat = test_input($_POST['straat']);
		} else {
		$error .= "- Vul uw straatnaam in. <br />";}
		
	if (!empty($_POST['huisnr'])) {
		$huisnr = test_input($_POST['huisnr']);
		} else {
		$error .= "- Vul uw huisnummer in. <br />";}
		
	if (!empty($_POST['postcode'])) {
		$postcode = test_input($_POST['postcode']);
		} else {
		$error .= "- Vul uw postcode in. <br />";}

	if (!empty($_POST['plaats'])) {
		$plaats = test_input($_POST['plaats']);
		} else {
		$error .= "- Vul uw woonplaats in. <br />";}
	
	if (!empty($_POST['land'])) {
		$land = test_input($_POST['land']);}
		
	if (!empty($_POST['email'])) {
		$email = $_POST['email'];
			if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){
      $error .= "- U heeft een ongeldig e-Mail adres ingevuld. <br/>";}
	} else {
		$error .= "- Vul uw e-Mail adres in. <br />";}
	
	if (!empty($_POST['telefoon'])) {
		$telefoon = test_input($_POST['telefoon']);}
	
	if (!empty($_POST['peradres'])) {
		$peradres = test_input($_POST['peradres']);}
	
	if (!empty($_POST['eerstehond'])) {
		$eerstehond = test_input($_POST['eerstehond']);}
	
	if (!empty($_POST['verderehond'])) {
		$verderehond = test_input($_POST['verderehond']);}
	
	if (!empty($_POST['babypup'])) {
		$babypup = test_input($_POST['babypup']);}
	
	if (!empty($_POST['koppelklas'])) {
		$koppelklas = test_input($_POST['koppelklas']);}
	
	if (!empty($_POST['koppelhond1'])) {
		$koppelhond1 = test_input($_POST['koppelhond1']);}
	
	if (!empty($_POST['koppelhond2'])) {
		$koppelhond2 = test_input($_POST['koppelhond2']);}
	
	if (!empty($_POST['totaal'])) {
		$totaal = test_input($_POST['totaal']);}
	
	if (!empty($_POST['naamjh'])) {
		$naamjh = test_input($_POST['naamjh']);}

	if (!empty($_POST['leeftijdjh'])) {
		$leeftijdjh = test_input($_POST['leeftijdjh']);}
	
	if (!empty($_POST['akkoord'])) {
		$akkoord = $_POST['akkoord'];
		} else {
		$error .= "- U moet akkoord gaan met de voorwaarden voordat u het bericht kunt versturen. <br />";}
	
		//no errors were set
		if(empty($error))
		{
			//code to send the email
    	//The form has been submitted, prep a nice thank you message
    	$output = '<center><b>Het Inschrijfformulier is verzonden <br />We zullen de gegevens verwerken <br/><u>Papillon & Phalène Vereniging Nederland</u></b></center>';
    	//Set the form flag to no display (cheap way!)
    	$flags = 'style="display:none;"';

    	//Deal with the email
    	$to = 'joke@pp-vn.nl';
		$from = $_POST['email'];
    	$subject = 'Inschrijfformulier';
		
		$message = 'From:   ' .$initialen .' ' .$eigachternaam . ' <' . $email . '>' ."\n\n";
    	$message .= 'Ras:   ' .$ras ."\n";
		$message .= 'Kleur:   ' .$kleur ."\n";
		$message .= 'Geslacht:   ' .$geslacht ."\n";
		$message .= 'Naam v/d hond:   ' .$naamhond ."\n";
		$message .= 'Stamboomnummer:   ' .$stamboom ."\n";
		$message .= 'Geboortedatum:   ' .$geboorte ."\n";
		$message .= 'Klasse:   ' .$klasse ."\n";
		$message .= 'Naam Fokker:   ' .$fokker ."\n"; 
		$message .= 'Naam Vaderhond:   ' .$vader ."\n";
		$message .= 'Naam Moederhond:   ' .$moeder ."\n";
		$message .= 'Eigenaar:   ' .$initialen .' ' . $eigachternaam ."\n";
		$message .= 'Mede-eigenaar:   ' .$minitialen .' ' .$machternaam ."\n";
		$message .= 'Adres:   ' .$straat .' ' .$huisnr .' ' .$postcode .' ' .$plaats .' ' .$land ."\n";
		$message .= 'Telefoon:   ' .$telefoon ."\n";
		$message .= 'e-Mail:   ' .$email ."\n";
		$message .= 'Per Adres:   ' .$peradres ."\n";
		$message .= 'Inschrijving eerste hond:   ' .$eerstehond . "\n";
		$message .= 'Andere honden ingeschreven:   ' .$verderehond . "\n";
		$message .= 'Baby- Puppyklasse:   ' .$babypup . "\n";
		$message .= 'Koppelklasse:   ' .$koppelklas . "\n";
		$message .= 'Koppelklasse Hond 1:   ' .$koppelhond1 . "\n";
		$message .= 'Koppelklasse Hond 2:   ' .$koppelhond2 . "\n";
		$message .= 'Totaalbedrag:   ' .$totaal . "\n";
		$message .= 'Naam Juniorhandler:   ' .$naamjh . "\n";
		$message .= 'Leeftijd Juniorhandler:   ' .$leeftijdjh . "\n";
		$message .= 'Akkoord:   ' .$akkoord ."\n";
    	$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
    	$filename = $_FILES['file']['name'];

    	$boundary =md5(date('r', time())); 
		
    	$headers = "From: fransien@pp-vn.nl";
    	$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";

    	$message="This is a multi-part message in MIME format.

--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

$message

--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--_1_$boundary--";
		
		
    	mail($to, $subject, $message, $headers);
		mail($from, $subject, $message, $headers); 
    }
		else
		{
			//display errors
			echo '<center><span class="error"><strong>Uw bericht is niet verstuurd<br/> De volgende fout(en) zijn opgetreden:</strong><br/>' . $error . '<br /><strong><u>Pas op: Bij een foutmelding indien nodig Kampioenstitel opnieuw toevoegen!!</u></strong></span></center>';
		} 
	}
?>

Link to comment
Share on other sites

i have this in my <head> tags

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

but the <head> tags are set below the PHP part of where it deals with checking the form and sending the message...

could that be the problem?

Link to comment
Share on other sites

i have this in my <head> tags

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

but the <head> tags are set below the PHP part of where it deals with checking the form and sending the message...

could that be the problem?

No, that shouldn't be the problem. They must be before the form is displayed so the correct character set should be submitted.

Link to comment
Share on other sites

thats what i thought too...

 

basically the page is set up like this:

 

- PHP forum check + mail send

- <head> tags where it sets the charset

- <body> tags with the forum in it

 

i also tried to set in the forum action : accept-characters utf-8... but that didnt work either

Link to comment
Share on other sites

Oh dam haha completely missed that :-\

 

It works now,

And I know its not the right way to do it... buut... since you are responding to this topic anyways

I might aswell ask another question.

Thats the last problem I would like to get out of it.

 

Its a form that can send an attachment.

But I've found that it will always send an empty attachment with the mail, whether you actually send an attachment or not.

Im fairly new to PHP so I guess theres a way to fix that aswell.

 

But I kinda dont know how...

Link to comment
Share on other sites

Where do I have to put that, because I tried to set it around this part:

$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
$filename = $_FILES['file']['name'];

But that didn't work... might be that I didn't include enough?

Link to comment
Share on other sites


$message="This is a multi-part message in MIME format.

--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

$message

--_2_$boundary--";
if ($attachment!='')
{
$message.="
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$attachment
--_1_$boundary--";
}
Link to comment
Share on other sites

While I am all for not reinventing the wheel I used a Pear package http://pear.php.net/package/Mail in a lot of places, but it is no longer being maintained and now I'm going to have to go back and replace and retest. Pieces of that code are depricated and those pieces were written that way largely (I think) to support features that I never used and probably never will.

 

I also hate debugging when I am using code as a black box. So, sometimes it is just easier to write it myself, know that I understand it, and know that I will not have complications from features that I don't need.

Link to comment
Share on other sites

PHPMailer is one of the biggest mailer libraries for PHP. It has been around since 13 years and is still actively maintained on GitHub. Do you expect your application to live longer than that?

 

The problem with home-made mail solutions is that they're usually wrong (header injection vulnerabilities are the rule rather than the exception) and massively bloat the code with all kinds of irrelevant low-level stuff. All the script above is supposed to do is send a friggin' mail with an attachment. Yet large parts of the code are actually dedicated to the inner workings of multipart messages.

 

In my opinion, this is a total waste of time and energy.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.