deluxes Posted April 14, 2015 Share Posted April 14, 2015 (edited) Hi. Basically I have some contact forms setup on a website, which submit the form data to an email address using PHP. The forms all use a similar structure (example of 1 below) and the same rules for sending mail. However, sometimes the form data is received, other times it is not. The form is submitted to an email address on the same domain as the website, but the mail server is not hosted (i.e. the MX rules point to a different mail server) On testing the forms myself, on various platforms and from different email addresses, I cannot find a problem, the email form is received each time I test. But there have been instances where the forms have not been received as a result of somebody phoning to say they haven't heard back from their website enquiry. I set up a gmail account to test over the period of a week and in fact did receive form data from one user into the gmail account but not into the 'MYDOMAIN' account, what would this suggest? Please can someone review the following code to ensure it is written correctly and there are no bugs that would cause this temperamental issue? I have other email forms on the website set up in a similar way and have tested numerous times but cannot reciprocate the problem to work out what is happening. Because it is temperamental (i.e. sometimes it works and other times it doesn't!) the problem is very difficult for me to diagnose, but it needs to be fixed. Is it a problem with my code or is it more likely to be a problem with the host, or mailserver? form.php - <form action="contact.php" method="post"> <table class="contacts" style="width:600px;"> <tr> <td width="300"><p style="font-weight:bold;margin:5px;">*Name:</p></td> <td><input type='text' size='40' name='yourname' /></td> </tr> <tr> <td width="300"><p style="font-weight:bold;margin:5px;">*Phone:</p></td> <td><input type='text' size='40' name='tel' /></td> </tr> <tr> <td width="300"><p style="font-weight:bold;margin:5px;">Company Name:</p></td> <td><input type='text' size='40' name='companyname' /></td> </tr> <tr> <td width="300" valign="top"><p style="font-weight:bold;margin:5px;">*Postal Address:</p></td> <td><textarea name='address' rows='5' style='resize:none;width:260px;'></textarea></td> </tr> <tr> <td width="300"><p style="font-weight:bold;margin:5px;">*Postal/ZIP Code:</p></td> <td width="300"><input type='text' size='40' name='postcode' /></td> </tr> <tr> <td><p style="font-weight:bold;margin:5px;">*Country:</p></td> <td> <select style='width:267px;' id="country" name="country"> <option value="Select from below" selected="selected">Select from below</option> <option value="Afghanistan">Afghanistan</option> <option value="Albania">Albania</option> <option value="Algeria">Algeria</option> <option value="American Samoa">American Samoa</option> <option value="Andorra">Andorra</option> <option value="Angola">Angola</option> <option value="Anguilla">Anguilla</option> <option value="Antarctica">Antarctica</option> <option value="Antigua & Barbuda">Antigua & Barbuda</option> <option value="Argentina">Argentina</option> <option value="Armenia">Armenia</option> <option value="Aruba">Aruba</option> <option value="Australia">Australia</option> <option value="Austria">Austria</option> <option value="Azerbaijan">Azerbaijan</option> <option value="Bahamas">Bahamas</option> <option value="Bahrain">Bahrain</option> <option value="Bangladesh">Bangladesh</option> <option value="Barbados">Barbados</option> <option value="Belarus">Belarus</option> <option value="Belgium">Belgium</option> <option value="Belize">Belize</option> <option value="Benin">Benin</option> <option value="Bermuda">Bermuda</option> <option value="Bhutan">Bhutan</option> <option value="Bolivia">Bolivia</option> <option value="Bosnia & Herzegovina">Bosnia & Herzegovina</option> <option value="Botswana">Botswana</option> <option value="Bouvet Island">Bouvet Island</option> <option value="Brazil">Brazil</option> <option value="British Indian Ocean Territory">British Indian Ocean Territory</option> <option value="Brunei Darussalam">Brunei Darussalam</option> <option value="Bulgaria">Bulgaria</option> <option value="Burkina Faso">Burkina Faso</option> <option value="Burundi">Burundi</option> <option value="Cambodia">Cambodia</option> <option value="Cameroon">Cameroon</option> <option value="Canada">Canada</option> <option value="Cape Verde">Cape Verde</option> <option value="Cayman Islands">Cayman Islands</option> <option value="Central African Republic">Central African Republic</option> <option value="Chad">Chad</option> <option value="Chile">Chile</option> <option value="China">China</option> <option value="Christmas Island">Christmas Island</option> <option value="Cocos (Keeling Islands)">Cocos (Keeling Islands)</option> <option value="Colombia">Colombia</option> <option value="Comoros">Comoros</option> <option value="Congo">Congo</option> <option value="Cook Islands">Cook Islands</option> <option value="Costa Rica">Costa Rica</option> <option value="Cote D'Ivoire (Ivory Coast)">Cote D'Ivoire (Ivory Coast)</option> <option value="Croatia">Croatia</option> <option value="Cuba">Cuba</option> <option value="Cyprus">Cyprus</option> <option value="Czech Republic">Czech Republic</option> <option value="Denmark">Denmark</option> <option value="Djibouti">Djibouti</option> <option value="Dominican Republic">Dominican Republic</option> <option value="Dominica">Dominica</option> <option value="East Timor">East Timor</option> <option value="Ecuador">Ecuador</option> <option value="Egypt">Egypt</option> <option value="El Salvador">El Salvador</option> <option value="Equatorial Guinea">Equatorial Guinea</option> <option value="Eritrea">Eritrea</option> <option value="Estonia">Estonia</option> <option value="Ethiopia">Ethiopia</option> <option value="Falkland Islands (Malvinas)">Falkland Islands (Malvinas)</option> <option value="Faroe Islands">Faroe Islands</option> <option value="Fiji">Fiji</option> <option value="Finland">Finland</option> <option value="France, Metropolitan">France, Metropolitan</option> <option value="France">France</option> <option value="French Guiana">French Guiana</option> <option value="French Polynesia">French Polynesia</option> <option value="French Southern Territories">French Southern Territories</option> <option value="Gabon">Gabon</option> <option value="Gambia">Gambia</option> <option value="Georgia">Georgia</option> <option value="Germany">Germany</option> <option value="Ghana">Ghana</option> <option value="Gibraltar">Gibraltar</option> <option value="Greece">Greece</option> <option value="Greenland">Greenland</option> <option value="Grenada">Grenada</option> <option value="Guadeloupe">Guadeloupe</option> <option value="Guam">Guam</option> <option value="Guatemala">Guatemala</option> <option value="Guinea-Bissau">Guinea-Bissau</option> <option value="Guinea">Guinea</option> <option value="Guyana">Guyana</option> <option value="Haiti">Haiti</option> <option value="Heard & McDonald Islands">Heard & McDonald Islands</option> <option value="Honduras">Honduras</option> <option value="Hong Kong">Hong Kong</option> <option value="Hungary">Hungary</option> <option value="Iceland">Iceland</option> <option value="India">India</option> <option value="Indonesia">Indonesia</option> <option value="Iran">Iran</option> <option value="Iraq">Iraq</option> <option value="Ireland">Ireland</option> <option value="Israel">Israel</option> <option value="Italy">Italy</option> <option value="Jamaica">Jamaica</option> <option value="Japan">Japan</option> <option value="Jordan">Jordan</option> <option value="Kazakhstan">Kazakhstan</option> <option value="Kenya">Kenya</option> <option value="Kiribati">Kiribati</option> <option value="Korea (North)">Korea (North)</option> <option value="Korea (South)">Korea (South)</option> <option value="Kuwait">Kuwait</option> <option value="Kyrgyzstan">Kyrgyzstan</option> <option value="Laos">Laos</option> <option value="Latvia">Latvia</option> <option value="Lebanon">Lebanon</option> <option value="Lesotho">Lesotho</option> <option value="Liberia">Liberia</option> <option value="Libya">Libya</option> <option value="Liechtenstein">Liechtenstein</option> <option value="Lithuania">Lithuania</option> <option value="Luxembourg">Luxembourg</option> <option value="Macau">Macau</option> <option value="Macedonia">Macedonia</option> <option value="Madagascar">Madagascar</option> <option value="Malawi">Malawi</option> <option value="Malaysia">Malaysia</option> <option value="Maldives">Maldives</option> <option value="Mali">Mali</option> <option value="Malta">Malta</option> <option value="Marshall Islands">Marshall Islands</option> <option value="Martinique">Martinique</option> <option value="Mauritania">Mauritania</option> <option value="Mauritius">Mauritius</option> <option value="Mayotte">Mayotte</option> <option value="Mexico">Mexico</option> <option value="Micronesia">Micronesia</option> <option value="Moldova">Moldova</option> <option value="Monaco">Monaco</option> <option value="Mongolia">Mongolia</option> <option value="Montenegro">Montenegro</option> <option value="Montserrat">Montserrat</option> <option value="Morocco">Morocco</option> <option value="Mozambique">Mozambique</option> <option value="Myanmar">Myanmar</option> <option value="Namibia">Namibia</option> <option value="Nauru">Nauru</option> <option value="Nepal">Nepal</option> <option value="Netherlands Antilles">Netherlands Antilles</option> <option value="Netherlands">Netherlands</option> <option value="New Caledonia">New Caledonia</option> <option value="New Zealand">New Zealand</option> <option value="Nicaragua">Nicaragua</option> <option value="Nigeria">Nigeria</option> <option value="Niger">Niger</option> <option value="Niue">Niue</option> <option value="Norfolk Island">Norfolk Island</option> <option value="Northern Mariana Islands">Northern Mariana Islands</option> <option value="Norway">Norway</option> <option value="Oman">Oman</option> <option value="Pakistan">Pakistan</option> <option value="Palau">Palau</option> <option value="Panama">Panama</option> <option value="Papua New Guinea">Papua New Guinea</option> <option value="Paraguay">Paraguay</option> <option value="Peru">Peru</option> <option value="Philippines">Philippines</option> <option value="Pitcairn">Pitcairn</option> <option value="Poland">Poland</option> <option value="Portugal">Portugal</option> <option value="Puerto Rico">Puerto Rico</option> <option value="Qatar">Qatar</option> <option value="Reunion">Reunion</option> <option value="Romania">Romania</option> <option value="Russian Federation">Russian Federation</option> <option value="Rwanda">Rwanda</option> <option value="S. Georgia & S. Sandwich Isls.">S. Georgia & S. Sandwich Isls.</option> <option value="Saint Kitts & Nevis">Saint Kitts & Nevis</option> <option value="Saint Lucia">Saint Lucia</option> <option value="Saint Vincent & The Grenadines">Saint Vincent & The Grenadines</option> <option value="Samoa">Samoa</option> <option value="San Marino">San Marino</option> <option value="Sao Tome & Principe">Sao Tome & Principe</option> <option value="Saudi Arabia">Saudi Arabia</option> <option value="Senegal">Senegal</option> <option value="Serbia">Serbia</option> <option value="Seychelles">Seychelles</option> <option value="Sierra Leone">Sierra Leone</option> <option value="Singapore">Singapore</option> <option value="Slovak Republic">Slovak Republic</option> <option value="Slovenia">Slovenia</option> <option value="Solomon Islands">Solomon Islands</option> <option value="Somalia">Somalia</option> <option value="South Africa">South Africa</option> <option value="Spain">Spain</option> <option value="Sri Lanka">Sri Lanka</option> <option value="St. Helena">St. Helena</option> <option value="St. Pierre & Miquelon">St. Pierre & Miquelon</option> <option value="Sudan">Sudan</option> <option value="Suriname">Suriname</option> <option value="Svalbard & Jan Mayen Islands">Svalbard & Jan Mayen Islands</option> <option value="Swaziland">Swaziland</option> <option value="Sweden">Sweden</option> <option value="Switzerland">Switzerland</option> <option value="Syria">Syria</option> <option value="Taiwan">Taiwan</option> <option value="Tajikistan">Tajikistan</option> <option value="Tanzania">Tanzania</option> <option value="Thailand">Thailand</option> <option value="Togo">Togo</option> <option value="Tokelau">Tokelau</option> <option value="Tonga">Tonga</option> <option value="Trinidad & Tobago">Trinidad & Tobago</option> <option value="Tunisia">Tunisia</option> <option value="Turkey">Turkey</option> <option value="Turkmenistan">Turkmenistan</option> <option value="Turks *amp; Caicos Islands">Turks & Caicos Islands</option> <option value="Tuvalu">Tuvalu</option> <option value="US Minor Outlying Islands">US Minor Outlying Islands</option> <option value="Uganda">Uganda</option> <option value="Ukraine">Ukraine</option> <option value="United Arab Emirates">United Arab Emirates</option> <option value="United Kingdom">United Kingdom</option> <option value="United States">United States</option> <option value="Uruguay">Uruguay</option> <option value="Uzbekistan">Uzbekistan</option> <option value="Vanuatu">Vanuatu</option> <option value="Vatican City State">Vatican City State</option> <option value="Venezuela">Venezuela</option> <option value="Vietnam">Vietnam</option> <option value="Virgin Islands (British)">Virgin Islands (British)</option> <option value="Virgin Islands (US)">Virgin Islands (US)</option> <option value="Wallis & Futuna Islands">Wallis & Futuna Islands</option> <option value="Western Sahara">Western Sahara</option> <option value="Yemen">Yemen</option> <option value="Zaire">Zaire</option> <option value="Zambia">Zambia</option> <option value="Zimbabwe">Zimbabwe</option> </select> </td> </tr> <tr> <td width="300"><p style="font-weight:bold;margin:5px;">*Email Address:</p></td> <td><input type='text' size='40' name='email' /></td> </tr> <tr> <td colspan="2"> <input type='hidden' name='subject' value='-General Enquiry' /> </td> </tr> <tr> <td colspan="2"> <script type="text/javascript"> <!--//start hiding document.write('<input type="hidden" name="ReferURL" value="' + cRefer + '">'); document.write('<input type="hidden" name="ReferDate" value="' + cDateRef + '">'); //end hiding--> </script> </td></tr> <tr> <td width="300" valign="top"><p style="font-weight:bold;margin:5px;">*Comments:</p></td> <td><textarea name='comments' rows='2' style='resize:none;width:260px;'></textarea></td> </tr> <tr> <td width="300"><p style="font-weight:bold;margin:5px;">How did you find our website?</p></td> <td width="300"> <select name="source" style="width:267px;" class="frmdet"> <option value="don't know" selected="selected">Don't Know</option> <option value="advertisement">Advertisement</option> <option value="surfing-google">Google Search</option> <option value="word-of-mouth">Recommendation</option> </select> </td> </tr> <tr> <td></td> <td><input type='submit' value='Submit' /><input type='reset' value='Clear' /></td> </tr> </table> </form> contact.php - (@MYDOMAIN.com and test@gmail.com is a placeholder for this code - I have removed my email address from below code to prevent spam) <?php /* Set e-mail recipient */ $myemail = "info@MYDOMAIN.com,test@gmail.com"; /* Check all form inputs using check_input function */ $yourname = check_input($_POST['yourname'], "Enter your name"); $tel = check_input($_POST['tel']); $email = check_input($_POST['email']); $companyname=$_POST['companyname']; $address=$_POST['address']; $postcode=$_POST['postcode']; $subject="$companyname"; $subject .=$_POST['subject']; $source=$_POST['source']; $country=$_POST['country']; $ReferURL=$_POST['ReferURL']; $comments = check_input($_POST['comments'], "Write your comments"); /* If e-mail is not valid show error message */ if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) { show_error("E-mail address not valid"); } /* Let's prepare the message for the e-mail */ $message = " <b>Name:</b> $yourname <br /> <b>Telephone:</b> $tel <br /> <b>E-mail:</b> $email <br /> <b>Company:</b> $companyname <br /> <b>Address:</b> $address <br /> <b>Postcode:</b> $postcode <br /> <b>Source:</b> $source <br /> <b>Message:</b> <br /> $comments <br /><br /> <b>Source:</b> $source <br /> <b>Referer:</b> $ReferURL <br /> "; /* Send the message using mail() function */ mail($myemail, $subject, $message,"Content-type:text/html\r\nFrom:".$email); echo "<p><b>Your message has been successfully sent. <a href=\"http://www.mywebsite.com\" title=\"Return to our homepage\">Return To The Homepage</a></b></p>"; /* Functions we used */ function check_input($data, $problem='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { show_error($problem); } return $data; } function show_error($myError) { ?> <b>Please correct the following error:</b><br /> <?php echo $myError; ?> <?php exit(); } ?> Website PHP Info: Core: Parameter Local Master mail.add_x_header On On mail.force_extra_parameters no value no value mail.log no value no value sendmail_from no value no value sendmail_path /usr/sbin/sendmail -t -i /usr/sbin/sendmail -t -i Standard: Path to sendmail /usr/sbin/sendmail -t -i Thanks in advance to anyone who may be able to assist. Edited April 14, 2015 by deluxes Quote Link to comment Share on other sites More sharing options...
joel24 Posted April 14, 2015 Share Posted April 14, 2015 Have you checked your spam/junkmail? You're setting the from email address to whatever value the customer enters... you should hardcode the from email address to an email address you own (and test that it works)... and if you want to be able to reply, you can add a "reply-to" header also. You're sending the emails from a web server which will have no matching DNS records for the SPF/DKIM check and will most likely be ending up in the spam box if it's trying to send an email from another domain which the SPF/DKIM can resolve. I'm not a wizard at the spam filter auth process though from a glance this would be my first suggestion (to check your spam filter). You should also have a if condition to check if the mail method worked correctly, currently you're just telling everyone that their email has been sent. /* Send the message using mail() function */ $success = mail($myemail, $subject, $message,"Content-type:text/html\r\nFrom:".$email); if ($success){ echo "<p><b>Your message has been successfully sent. <a href=\"http://www.mywebsite.com\" title=\"Return to our homepage\">Return To The Homepage</a></b></p>"; } else { //here you should set up logging to log the error so you can debug echo "<p><b>An error occurred when sending your email. If the problem persists please contact us on xxxx-xxxx"; } Quote Link to comment Share on other sites More sharing options...
deluxes Posted April 14, 2015 Author Share Posted April 14, 2015 Thanks for your assistance, that's great! I had considered the mailheaders, perhaps this was the issue all along! I'm not sure how I would write the errors to a log file, or better still, to email them directly to me so I then get on the case straight away?! Quote Link to comment Share on other sites More sharing options...
joel24 Posted April 14, 2015 Share Posted April 14, 2015 /* Send the message using mail() function */ $success = mail($myemail, $subject, $message,"Content-type:text/html\r\nFrom:".$email); if ($success){ echo "<p><b>Your message has been successfully sent. <a href=\"http://www.mywebsite.com\" title=\"Return to our homepage\">Return To The Homepage</a></b></p>"; } else { //send an email alerting you mail($myemail, "Email error", $message,"Content-type:text/html\r\nFrom:".$email); echo "<p><b>An error occurred when sending your email. If the problem persists please contact us on xxxx-xxxx"; } something like this should work, though it's a little unreliable as if there is an issue with the mail sending, it may not be able to send to you either. To setup a log, //add these to the top of your script. ini_set("log_errors", 1); ini_set("error_log", "path/to/your/log/file.log"); //then when you want to write a log entry error_log("This is the error message being written to the error.log"); you can read up about this error_log method here http://php.net/manual/en/function.error-log.php Quote Link to comment 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.