October 25, 201015 yr Hi all, Now my fingers have finally defrosted i can write this post. I've been tinkering with this contact form for ages now and it just won't work. It appears to send i.e. no errors are flagged up in code but the email doesnt arrive in my mailbox. I was wondering if it is an issue with the code or something not activated on the hosting? The site casn be foundhere As always your help is very much appreciated. Thanks OD
October 25, 201015 yr Who are you hosted with? Also, it could be that error reporting is disabled, in which case you wouldn't see the errors. Posting your code would also help
October 25, 201015 yr Author Thanks for the reply. Its hosted with NuBlue on one of their reseller packages. Just checked the error log and doesn't appear to show anything other than the lack of a favicon that ive forgotten to upload. The HTML is as follows: <form name="contactform" method="post" action="sendmail.php"> <ul> <li><span>Name</span><input type="text" name="visitor" maxlength="50" size="30"/></li> <li><span>Tel</span><input type="text" name="telephone" maxlength="30" size="30"/></li> <li><span>Email</span><input type="text" name="visitormail" maxlength="80" size="30"/></li> <li><span>Enquiry</span><textarea name="enquiry" maxlength="1000" cols="25" rows="6"></textarea></li> </ul> <input class="sendbutton" type="submit" value="Submit"/> </form> The PHP is <?php $visitor = $_POST['visitor']; $telephone = $_POST['telephone']; $visitormail = $_POST['visitormail']; $enquiry = $_POST['enquiry']; $sendTo = "alex@jumpcommunication.co.uk"; ?> <!--- Content =--> <?php if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,"."))) { include 'errorpage.php'; die; } if(empty($visitor) || empty($visitormail) || empty($enquiry) || empty($telephone)) { include 'errorpage.php'; die; } ?> <h4 id="mail">Thanks for contacting us <?php echo $visitor ?></h4><h4 id="mail">One of the team will be in touch soon</h4> <!--- /Content =--> <?php $todayis = date("l, F j, Y, g:i a") ; $subject = "Web Enquiry From Mobile Care"; $enquiry = stripcslashes($enquiry); $message = " $todayis [GMT] \n From: $visitor ($visitormail)\n Telephone: $telephone \n Enquiry: $enquiry \n "; $from = "From: $visitormail\r\n"; mail($sendTo, $subject, $message, $from); ?> Thanks again OD
October 25, 201015 yr Hi odmassive, This won't solve the problem you've got, but it will help you identify the problems you will definitely have with spam using the validation you've currently got in place. Basic Validation At present you don't check for minimum/maximum string lengths on any of the fields. I could successfully submit your form with a . in each input (except email which I go into more detail about below). You'd at least want to check that name/telephone were of a reasonable length and contained valid characters. Telephone Validation You could use something similar to this to ensure the phone number submitted was numeric (after stripping out +, -, (, ), and white space which I'd consider valid in the case of a telephone number). if(is_numeric(str_replace(array('+', '-', '(', ')', ' '), '', $telephone))){ echo 'This is a telephone number!'; } Email Validation At the moment your email validation just checks for an @ and a . - This means I could successfully send a message if $visitormail == '@.'. A combination of filter_var() and checkdnsrr() would be a much more effective validation method. Failing that you could use Michael Rishton's email validation regex, the same expression used in PHP's logical filters C file. Error Reporting While in development, make sure error reporting and display errors are on... Always... // Show ALL errors ini_set('display_errors', 1); error_reporting(-1); So... To start with I'd implement some proper validation as I've outlined above, then make sure error_reporting is turned on and go from there.
October 25, 201015 yr using mail() is unreliable. It may well be sending but your email client might be flagging it as spam, especially as you're sending it from the users email address, which your local sendmail server is not authoritative for. Try sending it from noreply@mobilecareltd.co.uk and set the Reply-To header from the enquirer. Also try sending a test to a hotmail/live address, they will accept them but put them in the junk folder.
October 25, 201015 yr While in development, make sure error reporting and display errors are on... Always... // Show ALL errors ini_set('display_errors', 0); error_reporting(-1); That would actually turn the errors and error reporting off. They should be // Show ALL errors ini_set('display_errors', 1); error_reporting(E_ALL); Don't seem to be able to pick up what's wrong with the form though, perhaps the error reporting being on will give more information
October 25, 201015 yr That would actually turn the errors and error reporting off. They should be // Show ALL errors ini_set('display_errors', 1); error_reporting(E_ALL); Don't seem to be able to pick up what's wrong with the form though, perhaps the error reporting being on will give more information My bad, corrected the ini_set after I posted it. The error_reporting is correct, -1 will report all errors, forever
October 25, 201015 yr Author Thanks for all your feedback. I've made some of the changes as you've suggested, the code is now as follows <?php // Show ALL errors ini_set('display_errors', 1); error_reporting(E_ALL); $visitor = $_POST['visitor']; $telephone = $_POST['telephone']; $visitormail = $_POST['visitormail']; $enquiry = $_POST['enquiry']; $sendTo = "alex@jumpcommunication.co.uk"; ?> <!--- Content =--> <?php if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,"."))) { include 'errorpage.php'; die; } if(empty($visitor) || empty($visitormail) || empty($enquiry) || empty($telephone)) { include 'errorpage.php'; die; } ?> <h4 id="mail">Thanks for contacting us <?php echo $visitor ?></h4><h4 id="mail">One of the team will be in touch soon</h4> <!--- /Content =--> <?php $todayis = date("l, F j, Y, g:i a") ; $subject = "Web Enquiry From Mobile Care"; $enquiry = stripcslashes($enquiry); $message = " $todayis [GMT] \n From: $visitor ($visitormail)\n Telephone: $telephone \n Enquiry: $enquiry \n "; $from = "noreply@mobilecareltd.co.uk"; mail($sendTo, $subject, $message, $from); ?> I changed the sendto email to an aol address and it worked fine but as soon as I changed it to the original email it fails again. Ive been reading about headers...do they need to be added? Also is there a better way of sending other than mail()? I'm new to php so apologies for the stupidity. Thanks again OD
October 25, 201015 yr Thanks for all your feedback. I've made some of the changes as you've suggested, the code is now as follows <?php // Show ALL errors ini_set('display_errors', 1); error_reporting(E_ALL); $visitor = $_POST['visitor']; $telephone = $_POST['telephone']; $visitormail = $_POST['visitormail']; $enquiry = $_POST['enquiry']; $sendTo = "alex@jumpcommunication.co.uk"; ?> <!--- Content =--> <?php if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,"."))) { include 'errorpage.php'; die; } if(empty($visitor) || empty($visitormail) || empty($enquiry) || empty($telephone)) { include 'errorpage.php'; die; } ?> <h4 id="mail">Thanks for contacting us <?php echo $visitor ?></h4><h4 id="mail">One of the team will be in touch soon</h4> <!--- /Content =--> <?php $todayis = date("l, F j, Y, g:i a") ; $subject = "Web Enquiry From Mobile Care"; $enquiry = stripcslashes($enquiry); $message = " $todayis [GMT] \n From: $visitor ($visitormail)\n Telephone: $telephone \n Enquiry: $enquiry \n "; $from = "noreply@mobilecareltd.co.uk"; mail($sendTo, $subject, $message, $from); ?> I changed the sendto email to an aol address and it worked fine but as soon as I changed it to the original email it fails again. Ive been reading about headers...do they need to be added? Also is there a better way of sending other than mail()? I'm new to php so apologies for the stupidity. Thanks again OD You can try my contact form script and see if it sends with that email address Rizo Contact
October 25, 201015 yr http://phpmailer.worxware.com/ That seems to be the best php mailing class around, I've never had an issue with using it
October 25, 201015 yr http://phpmailer.worxware.com/ That seems to be the best php mailing class around, I've never had an issue with using it I've never used it myself but have heard good things about this class also
October 25, 201015 yr It's fantastic. HTML emails are a cinch with it. possibly the best HTML email class around
October 25, 201015 yr Try using standard RFC 2822 headers. <?php $todayis = date("l, F j, Y, g:i a") ; $subject = "Web Enquiry From Mobile Care"; $enquiry = stripcslashes($enquiry); $message = " $todayis [GMT] \n From: $visitor ($visitormail)\n Telephone: $telephone \n Enquiry: $enquiry \n "; $message = wordwrap($message, 70); $from = 'From: noreply@mobilecareltd.co.uk' . "\r\n" . 'Reply-To: '.$visitormail; mail($sendTo, $subject, $message, $from); ?>
Create an account or sign in to comment