deluxes Posted September 30, 2016 Share Posted September 30, 2016 (edited) Hi, I have some contact forms on our website. For some time, submissions are parsed via our web hosts' php sendmail function. We have had issues where some (but not all) users who submit contact forms via our website the emails are not received by us. One of our forms sends an auto-responder to the user, as well as the submission to us. When I contacted our webhost they advised: SMTP Authentication can cause the issue as if you are sending without the authentication it's just like you are "spoofing" the emails and some spam filters on the receiving end can block these emails as mistakenly flagged as spam or at least as suspicious. So I want to send all form data via SMTP from our own webserver, but I'm not sure exactly how we go about this. Here is the PHP for the form with the auto-responder. Can someone advise how I can get this to send the content via SMTP? Is it just a simple case of configuring in form-output.php ? (Email addresses, domains/IP's etc. omitted from script for security reasons) form-output.php: <?php /* Set e-mail recipient */ $myemail = "email@email.com"; /* Check all form inputs using check_input function */ $thename = check_input($_POST['thename'], "Enter your name"); $tel = check_input($_POST['tel'], "Enter your telephone number"); $email = check_input($_POST['email'], "Enter your email address"); $companyname=$_POST['companyname']; $address=$_POST['address']; $method=$_POST['method']; $postcode=$_POST['postcode']; if (is_array($_POST['brochure_type'])) { $brochure_type = implode(', ', $_POST['brochure_type']); } else { $brochure_type = $_POST['brochure_type']; } $subject="$companyname"; $subject .=$_POST['subject']; $source=$_POST['source']; $country=$_POST['country']; $ReferURL=$_POST['ReferURL']; $ReferDate=$_POST['ReferDate']; /* 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> $thename <br /> <b>Preferred Method:</b> $method <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>Selected Brochures:</b> $brochure_type <br /> <b>Source:</b> $source <br /> <b>Referer:</b> $ReferURL <br /> <b>Refer Date:</b> $ReferDate <br /> "; $headers = "Content-type:text/html;charset=UTF-8" . "\r\n"; $headers .= "From: $myemail" . "\r\n"; $headers .= "Reply-To: $email" . "\r\n"; /* Send the message using mail() function */ $success = mail($myemail, $subject, $message,$headers); if ($success){ echo "<p><b>Your message has been successfully sent. <a href=\"http://www.mydomain.com\" title=\"Return to our homepage\">Return To The Homepage</a></b></p>"; } else { echo "<p><b>An error occurred when sending your email. If the problem persists please contact us on 01869 238400"; } /* 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(); } ?> <!---Responding Email--> <?php /* Auto Responder Version 2 */ if(isset($_POST) && ($_POST['send'] == 1)){ $brochures = array( 'Product Brochure 1' => 'https://www.mydoamin.com/brochure1', 'Product Brochure 2' => 'https://www.mydoamin.com/brochure2', 'Product Brochure 3' => 'https://www.mydoamin.com/brochure3', ); $subject = 'Your Brochure'; $message = "Dear $thename,\n\n Thank you for your interest in our products. Please click or paste the link(s) below into your browser to view our product catalogue(s). If you requested a hard copy then this will be sent to you.\n\n"; if(isset($_POST['brochure_type']) && count($_POST['brochure_type']) > 0){ foreach($_POST['brochure_type'] as $doc){ if(isset($brochures[$doc])){ $message .= "$doc Brochure: ".$brochures[$doc]."\n"; } } } $message .= "\n Please do not hesitate to contact us if you require any further information.\n\n "; $headers = 'From: email@email.com' . "\r\n" . 'Reply-To: email@email.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($email, $subject, $message, $headers); } ?> form.php <form action="brochuresubmit.php" method="post"> <div class="form-group"> <table class="contacts"> <tr> <td>*Name:</td> <td><input type='text' class="form-control" name='thename' /></td> </tr> <tr> <td>*Preferred Format:</td> <td><select name='method' class="form-control" > <option value='Hard Copy'>Hard Copy</option><option value='Electronic Copy'>Electronic Copy</option> </select></td> </tr> <tr> <td>Company Name:</td> <td><input type='text' class="form-control" name='companyname' /></td> </tr> <tr> <td valign="top">*Address:</td> <td><textarea name='address' rows='5' class="form-control" style='resize:none;width:260px;'></textarea></td> </tr> <tr> <td>*Postal Code:</td> <td><input type='text' class="form-control" name='postcode' /></td> </tr> <tr> <td>*Country:</td> <td> <select id="country" class="form-control" 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>*Telephone:</td> <td><input type='text' class="form-control" name='tel' /></td> </tr> <tr> <td>*Email Address:</td> <td><input type='text' class="form-control" name='email' /></td> </tr> <tr> <td>Select Brochure(s) you would like to receive a copy of:</td> <td><p><input type='checkbox' name='brochure_type[]' value='TorqSense' /> TorqSense<br /> <input type='checkbox' name='brochure_type[]' value='LoadSense' /> LoadSense<br /> <input type='checkbox' name='brochure_type[]' value='HeliNav' /> HeliNav</td> </tr> <td>How did you find us?</td> <td> <select name="source" class="form-control" 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="surfing-bing">Bing Search</option> <option value="surfing-yahoo">Yahoo Search</option> <option value="Existing Customer">Existing Customer</option> <option value="magazine">Magazine Article</option> <option value="facebook">Facebook</option> <option value="twitter">Twitter</option> <option value="Linked In">Linked In</option> <option value="Google Plus">Google+ Social Network</option> <option value="referral">Personal Referral</option> <option value="Other">Trade Show</option> </select> </td> </tr> <tr> <td colspan="2"> <input type='hidden' name='subject' value='-Brochure Request' /> </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></td> <td> <input type="hidden" name="send" value="1" /> <input type="submit" value="Send" /><input type='reset' value='Clear' /></td> </tr> </table> </div> </form> Edited September 30, 2016 by deluxes Quote Link to comment https://forums.phpfreaks.com/topic/302259-send-mail-forms-via-smtp/ Share on other sites More sharing options...
ginerjm Posted September 30, 2016 Share Posted September 30, 2016 Why not separate the two? Use php to grab the form's data (a basic simple approach) and then build a structured message of some sort (your choice) and just send that as a simple text email. Quote Link to comment https://forums.phpfreaks.com/topic/302259-send-mail-forms-via-smtp/#findComment-1537922 Share on other sites More sharing options...
Destramic Posted October 1, 2016 Share Posted October 1, 2016 I'd try to use phpmailer instead of php's mail function. https://github.com/PHPMailer/PHPMailer Or why not allow the contact data to be inserted into a db table?...atleast that way you know your going to receive it Quote Link to comment https://forums.phpfreaks.com/topic/302259-send-mail-forms-via-smtp/#findComment-1537956 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.