soc86 Posted September 9, 2011 Share Posted September 9, 2011 Contact Form Not Working, Help please Postby soc86 » Thu Sep 08, 2011 1:33 pm Hi, I am a bit of a novice in PHP, but i am having blank emails sent to my email address, is anyone able to tell me where i am going wrong? My codes below: PHP <?php $field_name = $_POST['full_name']; $field_email = $_POST['email']; $field_telephone = $_POST['telephone']; $field_message = $_POST['message']; $mail_to = 'shaun_o_c@yahoo.co.uk'; $subject = 'Contact form'.$field_name; $body_message = 'From: '.$field_name."\n"; $body_message .= 'E-mail: '.$field_email."\n"; $body_message .= 'Message: '.$field_message; $headers = 'From: '.$cf_email."\r\n"; $headers .= 'Reply-To: '.$cf_email."\r\n"; $mail_status = mail($mail_to, $subject, $body_message, $headers); if ($mail_status) { ?> <script language="javascript" type="text/javascript"> alert('Thank You.'); window.location = 'index-4.html'; </script> <?php } else { ?> <script language="javascript" type="text/javascript"> alert('Fail ba'); window.location = 'index-4.html'; </script> <?php } ?> HTML <h2>Contact Form</h2> <form id="ContactForm" action="contact.php" enctype="multipart/form-data"> <div> <div class="rows"><input name="full_name" class="input" type="text" value="Name:" onblur="if(this.value=='') this.value='Enter your Name:'" onFocus="if(this.value =='Enter your Name:' ) this.value=''" /></div> <div class="rows"><input class="email" type="text" value="E-mail :" onblur="if(this.value=='') this.value='Enter your E-mail:'" onFocus="if(this.value =='Enter your E-mail:' ) this.value=''" /></div> <div class="rows"><input class="relephone" type="text" value="Telephone (optional):" onblur="if(this.value=='') this.value='Enter your Phone:'" onFocus="if(this.value =='Enter your Phone:' ) this.value=''" /></div> <div class="textarea_box"><textarea name="message" cols="1" rows="1" onBlur="if(this.value=='') this.value='Enter your Message:'" onFocus="if(this.value =='Enter your Message:' ) this.value=''" >Skriv inn din melding:</textarea></div> <a href="#" class="link1" onClick="document.getElementById('ContactForm').reset()">Reset</a> <a href="#" class="link1" onClick="document.getElementById('ContactForm').submit()">Send</a> </div> </form> Quote Link to comment https://forums.phpfreaks.com/topic/246773-contact-form-not-working-help-please/ Share on other sites More sharing options...
9mileshq Posted September 9, 2011 Share Posted September 9, 2011 Well if your getting an email through, somethings going right! But it looks like you aren't specifying any headers $headers = 'From:' . $to . '' . "\r\n" . 'Reply-To: ' . $to . '' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; mail($email, $subject, $message, $headers); I can that you are missing these above - try putting them into your script (obviously not exactly as they are, change the variables) and that should hopefully bring some content into them blank emails! any questions follow us on twitter and we will be happy to help www.twitter.com/9mileshq Quote Link to comment https://forums.phpfreaks.com/topic/246773-contact-form-not-working-help-please/#findComment-1267279 Share on other sites More sharing options...
Adam Posted September 9, 2011 Share Posted September 9, 2011 Check the contents of $body_message with var_dump - it may just be that you're not actually sending anything. As 9mileshq pointed out you're missing some headers, more importantly the content-type header, which if not set it's likely that the email client will just display the HTML as plain-text. Also I wouldn't use JavaScript to display the result. You can't rely on the user having it enabled, which means they wouldn't be redirected and you just display a blank page breaking the navigation. PHP allows you to pass a location header with the response that will tell the browser to redirect to another page: header('Location: index-4.html'); You will probably never encounter a situation where this method doesn't work, where as users with JS disabled is somewhat more common. Quote Link to comment https://forums.phpfreaks.com/topic/246773-contact-form-not-working-help-please/#findComment-1267284 Share on other sites More sharing options...
soc86 Posted September 9, 2011 Author Share Posted September 9, 2011 Sorry, but i dont follow? Do i need to add: $headers = 'From:' . $to . '' . "\r\n" .'Reply-To: ' . $to . '' . "\r\n" .'X-Mailer: PHP/' . phpversion();$headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";mail($email, $subject, $message, $headers);() to the HTML or my PHP? and what part of the code/HTML? Many Thanks in advance!! Quote Link to comment https://forums.phpfreaks.com/topic/246773-contact-form-not-working-help-please/#findComment-1267482 Share on other sites More sharing options...
Pikachu2000 Posted September 9, 2011 Share Posted September 9, 2011 Can you clarify the problem a bit? Is it that you're randomly getting blank emails, or you're actually filling out the form, submitting it, and the resulting email is blank? As it stands right now, all it takes to generate a blank email is for a search engine spider (or anything at all) to simply load the page, and a blank email will be sent because there's no logic to check that the form has been submitted and contains valid data before sending the email. Additionally, there's nothing to prevent email header injection, so the form is wide open for use as a spammer's wet dream. Quote Link to comment https://forums.phpfreaks.com/topic/246773-contact-form-not-working-help-please/#findComment-1267485 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.