kimikai Posted July 2, 2014 Share Posted July 2, 2014 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>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/289370-form-does-not-accept-characters-like-%C3%A9-etc/ Share on other sites More sharing options...
davidannis Posted July 2, 2014 Share Posted July 2, 2014 You need to allow a character set that supports those characters in your html header Instructions are here. http://www.w3.org/International/O-charset Quote Link to comment https://forums.phpfreaks.com/topic/289370-form-does-not-accept-characters-like-%C3%A9-etc/#findComment-1483521 Share on other sites More sharing options...
kimikai Posted July 2, 2014 Author Share Posted July 2, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/289370-form-does-not-accept-characters-like-%C3%A9-etc/#findComment-1483523 Share on other sites More sharing options...
davidannis Posted July 2, 2014 Share Posted July 2, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289370-form-does-not-accept-characters-like-%C3%A9-etc/#findComment-1483524 Share on other sites More sharing options...
kimikai Posted July 2, 2014 Author Share Posted July 2, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/289370-form-does-not-accept-characters-like-%C3%A9-etc/#findComment-1483525 Share on other sites More sharing options...
davidannis Posted July 2, 2014 Share Posted July 2, 2014 In your email header you use a different encoding. Content-Type: text/plain; charset=\"iso-8859-1\" I would try to make the encoding consistent. Quote Link to comment https://forums.phpfreaks.com/topic/289370-form-does-not-accept-characters-like-%C3%A9-etc/#findComment-1483526 Share on other sites More sharing options...
kimikai Posted July 2, 2014 Author Share Posted July 2, 2014 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... Quote Link to comment https://forums.phpfreaks.com/topic/289370-form-does-not-accept-characters-like-%C3%A9-etc/#findComment-1483529 Share on other sites More sharing options...
davidannis Posted July 2, 2014 Share Posted July 2, 2014 Try wrapping the part that sends the attachment in an if if ($attachment!=''){ $message .= STUFF YOU ALREADY HAVE HERE } instead of having it included in the message every time. Quote Link to comment https://forums.phpfreaks.com/topic/289370-form-does-not-accept-characters-like-%C3%A9-etc/#findComment-1483533 Share on other sites More sharing options...
kimikai Posted July 2, 2014 Author Share Posted July 2, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/289370-form-does-not-accept-characters-like-%C3%A9-etc/#findComment-1483536 Share on other sites More sharing options...
davidannis Posted July 3, 2014 Share Posted July 3, 2014 $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--"; } Quote Link to comment https://forums.phpfreaks.com/topic/289370-form-does-not-accept-characters-like-%C3%A9-etc/#findComment-1483679 Share on other sites More sharing options...
Jacques1 Posted July 3, 2014 Share Posted July 3, 2014 (edited) Do you guys seriously try to manually assemble a multipart message? There are libraries for mailing! Edited July 3, 2014 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/289370-form-does-not-accept-characters-like-%C3%A9-etc/#findComment-1483694 Share on other sites More sharing options...
davidannis Posted July 3, 2014 Share Posted July 3, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289370-form-does-not-accept-characters-like-%C3%A9-etc/#findComment-1483717 Share on other sites More sharing options...
Jacques1 Posted July 3, 2014 Share Posted July 3, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289370-form-does-not-accept-characters-like-%C3%A9-etc/#findComment-1483728 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.