-4

I am very new to PHP and I am trying to get my php code for a contact form to always send the info via email. My issue is that it sends maybe half the time and I have no idea why.

    <?php


if(isset($_POST['Email_Address'])) {

    include 'freecontactformsettings.php';

    function died($error) {
        echo "Sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    if(!isset($_POST['Full_Name']) ||
        !isset($_POST['Email_Address']) ||
        !isset($_POST['Zip']) ||
        !isset($_POST['Telephone_Number']) ||
        !isset($_POST['AntiSpam'])      
        ) {
        died('Sorry, there appears to be a problem with your form submission.');        
    }

    $Model = $_POST['Model']; // not required
    $full_name = $_POST['Full_Name']; // required
    $email_from = $_POST['Email_Address']; // required
    $Zip = $_POST['Zip']; // required
    $telephone = $_POST['Telephone_Number']; // not required
    $Haypod = $_POST['Haypod']; // not required
    $Hayrack = $_POST['Hayrack']; // not required
    $Satellite = $_POST['Satellite']; // not required
    $Slide_Out = $_POST['Slide_Out']; // not required
    $Generator = $_POST['Generator']; // not required
    $Rear_Ramp = $_POST['Rear_Ramp']; // not required
    $Power_Awning = $_POST['Power_Awning']; // not required
    $Integrated_Haypod = $_POST['Integrated_Haypod']; // not required
    $Carpeted_Rear_Tack = $_POST['Carpeted_Rear_Tack']; // not required
    $Full_Front_Riser_Wall = $_POST['Full_Front_Riser_Wall']; // not required
    $Reverse_Load_Package = $_POST['Reverse_Load_Package']; // not required
    $Rear_Side_Load_Package = $_POST['Rear_Side_Load_Package']; // not required
    $Drop_Down_Butt_Windows = $_POST['Drop_Down_Butt_Windows']; // not required
    $Plant_Made_Entrance_Door = $_POST['Plant_Made_Entrance_Door']; // not required
    $Angled_Flow_Thru_Divider = $_POST['Angled_Flow_Thru_Divider']; // not required
    $Escape_Door = $_POST['Escape_Door']; // not required
    $Alder_Interior = $_POST['Alder_Interior']; // not required
    $Hickory_Interior = $_POST['Hickory_Interior']; // not required
    $Chestnut_Interior = $_POST['Chestnut_Interior']; // not required
    $Angled_Air_Flow_Divider = $_POST['Angled_Air_Flow_Divider']; // not required
    $Stud_Divider = $_POST['Stud_Divider']; // not required
    $Goodyear_E_Range_Aluminum_Rims = $_POST['Goodyear_E_Range_Aluminum_Rims']; // not required
    $Rear_Tack = $_POST['Rear_Tack']; // not required
    $Full_Running_Boards = $_POST['Full_Running_Boards']; // not required
    $Additional_Clearance_Lights = $_POST['Additional_Clearance_Lights']; // not required
    $Spare_Tire = $_POST['Spare_Tire']; // not required
    $Lined_Insulated_Dressing_Room = $_POST['Lined_Insulated_Dressing_Room']; // not required
    $lined_insulated_horse_area = $_POST['lined_insulated_horse_area']; // not required
    $Electric_Jack = $_POST['Electric_Jack']; // not required
    $Colored_Skin = $_POST['Colored_Skin']; // not required
    $Hay_Rack = $_POST['Hay_Rack']; // not required
    $Drop_Butt_Windows = $_POST['Drop_Butt_Windows']; // not required
    $Saddle_Bosses = $_POST['Saddle_Bosses']; // not required
    $Additional_Bridle_Hooks = $_POST['Additional_Bridle_Hooks']; // not required
    $Additional_Blanket_Bars = $_POST['Additional_Blanket_Bars']; // not required
    $Foal_Divider = $_POST['Foal_Divider']; // not required
    $Polished_Nose = $_POST['Polished_Nose']; // not required
    $Feed_Bags = $_POST['Feed_Bags']; // not required
    $Hydraulic_Jack = $_POST['Hydraulic_Jack']; // not required
    $Extruded_Slat_Horse_Area = $_POST['Extruded_Slat_Horse_Area']; // not required
    $Bottom_Stall_Divider = $_POST['Bottom_Stall_Divider']; // not required
    $Exterior_Package = $_POST['Exterior_Package']; // not required
    $Entertainment_Package = $_POST['Entertainment_Package']; // not required
    $Towing_Package = $_POST['Towing_Package']; // not required
    $Furnace = $_POST['Furnace']; // not required
    $Microwave = $_POST['Microwave']; // not required
    $Pass_Thru_Door = $_POST['Pass_Thru_Door']; // not required
    $AC_13500_BTU_With_Heat_Strip = $_POST['AC_13500_BTU_With_Heat_Strip']; // not required
    $Bathroom_Skylight = $_POST['Bathroom_Skylight']; // not required
    $Bath_Sink = $_POST['Bath_Sink']; // not required
    $Horse_Area_Package = $_POST['Horse_Area_Package']; // not required
    $Interior_Package = $_POST['Interior_Package']; // not required
    $Full_Length_Extruded_Slat = $_POST['Full_Length_Extruded_Slat']; // not required
    $Individual_Butt_Windows_30x26 = $_POST['Individual_Butt_Windows_30x26']; // not required
    $Front_Full_Riser_Wall = $_POST['Front_Full_Riser_Wall']; // not required
    $Dual_Hydraulic_Jacks_8_Wide = $_POST['Dual_Hydraulic_Jacks_8_Wide']; // not required
    $Single_Leg_Hydraulic_Jack_6_9_Wide = $_POST['Single_Leg_Hydraulic_Jack_6_9_Wide']; // not required
    $Hickory_Interior_Package = $_POST['Hickory_Interior_Package']; // not required
    $Nutmeg_Interior_Package = $_POST['Nutmeg_Interior_Package']; // not required
    $comments = $_POST['Your_Message']; // not required
    $antispam = $_POST['AntiSpam']; // required

    $error_message = "";

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(preg_match($email_exp,$email_from)==0) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }
    if(strlen($full_name) < 2) {
    $error_message .= 'Your Name does not appear to be valid.<br />';
    }

    if($antispam <> $antispam_answer) {
    $error_message .= 'The Anti-Spam answer you entered is not correct.<br />';
    }

    if(strlen($error_message) > 0) {
    died($error_message);
    }
    $email_message = "Trailer Quote Request\r\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Model: ".clean_string($Model)."\r\n";
    $email_message .= "Full Name: ".clean_string($full_name)."\r\n";
    $email_message .= "Email: ".clean_string($email_from)."\r\n";
    $email_message .= "Zip: ".clean_string($Zip)."\r\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\r\n";

    //beginning optional fields

    if($_POST['Haypod']) {
        $email_message .= "Haypod: ".clean_string($Haypod)."\r\n";
    } else {
        empty($Haypod);
    }

    if($_POST['Hayrack']) {
        $email_message .= "Hayrack: ".clean_string($Hayrack)."\r\n";
    } else {
        empty($Hayrack);
    }

    if($_POST['Satellite']) {
        $email_message .= "Satellite: ".clean_string($Satellite)."\r\n";
    } else {
        empty($Satellite);
    }

    if($_POST['Slide_Out']) {
        $email_message .= "Slide Out: ".clean_string($Slide_Out)."\r\n";
    } else {
        empty($Slide_Out);
    }

    if($_POST['Generator']) {
        $email_message .= "Generator: ".clean_string($Generator)."\r\n";    
    } else {
        empty($Generator);
    }   

    if($_POST['Rear_Ramp']) {
        $email_message .= "Rear Ramp: ".clean_string($Rear_Ramp)."\r\n";
    } else {
        empty($Rear_Ramp);
    }

    if($_POST['Power_Awning']) {
        $email_message .= "Power Awning: ".clean_string($Power_Awning)."\r\n";
    } else {
        empty($Power_Awning);
    }

    if($_POST['Integrated_Haypod']) {
        $email_message .= "Integrated Haypod: ".clean_string($Integrated_Haypod)."\r\n";
    } else {
        empty($Integrated_Haypod);
    }

    if($_POST['Carpeted_Rear_Tack']) {
        $email_message .= "Carpeted Rear Tack: ".clean_string($Carpeted_Rear_Tack)."\r\n";
    } else {
        empty($Carpeted_Rear_Tack);
    }

    if($_POST['Full_Front_Riser_Wall']) {
        $email_message .= "Full Front Riser Wall: ".clean_string($Full_Front_Riser_Wall)."\r\n";
    } else {
        empty($Full_Front_Riser_Wall);
    }

    if($_POST['Reverse_Load_Package']) {
        $email_message .= "Reverse Load Package: ".clean_string($Reverse_Load_Package)."\r\n";
    } else {
        empty($Reverse_Load_Package);
    }

    if($_POST['Rear_Side_Load_Package']) {
        $email_message .= "Rear Side Load Package: ".clean_string($Rear_Side_Load_Package)."\r\n";
    } else {
        empty($Rear_Side_Load_Package);
    }

    if($_POST['Angled_Flow_Thru_Divider']) {
        $email_message .= "Angled Flow Thru Divider: ".clean_string($Angled_Flow_Thru_Divider)."\r\n";
    } else {
        empty($Angled_Flow_Thru_Divider);
    }

    if($_POST['Drop_Down_Butt_Windows']) {
        $email_message .= "Drop Down Butt Windows: ".clean_string($Drop_Down_Butt_Windows)."\r\n";
    } else {
        empty($Drop_Down_Butt_Windows);
    }

    if($_POST['Plant_Made_Entrance_Door']) {
        $email_message .= "Plant Made Entrance Door: ".clean_string($Plant_Made_Entrance_Door)."\r\n";
    } else {
        empty($Plant_Made_Entrance_Door);
    }

    if($_POST['Escape_Door']) {
        $email_message .= "Escape Door: ".clean_string($Escape_Door)."\r\n";
    } else {
        empty($Escape_Door);
    }

    if($_POST['Alder_Interior']) {
        $email_message .= "Alder Interior: ".clean_string($Alder_Interior)."\r\n";
    } else {
        empty($Alder_Interior);
    }

    if($_POST['Hickory_Interior']) {
        $email_message .= "Hickory Interior: ".clean_string($Hickory_Interior)."\r\n";
    } else {
        empty($Hickory_Interior);
    }

    if($_POST['Chestnut_Interior']) {
        $email_message .= "Chestnut Interior: ".clean_string($Chestnut_Interior)."\r\n";
    } else {
        empty($Chestnut_Interior);
    }

    if($_POST['Angled_Air_Flow_Divider']) {
        $email_message .= "Angled Air Flow Divider: ".clean_string($Angled_Air_Flow_Divider)."\r\n";
    } else {
        empty($Angled_Air_Flow_Divider);
    }

    if($_POST['Stud_Divider']) {
        $email_message .= "Stud Divider: ".clean_string($Stud_Divider)."\r\n";
    } else {
        empty($Stud_Divider);
    }

    if($_POST['Goodyear_E_Range_Aluminum_Rims']) {
        $email_message .= "Goodyear E Range Aluminum Rims: ".clean_string($Goodyear_E_Range_Aluminum_Rims)."\r\n";
    } else {
        empty($Goodyear_E_Range_Aluminum_Rims);
    }

    if($_POST['Rear_Tack']) {
        $email_message .= "Rear Tack: ".clean_string($Rear_Tack)."\r\n";
    } else {
        empty($Rear_Tack);
    }

    if($_POST['Full_Running_Boards']) {
        $email_message .= "Full Running Boards: ".clean_string($Full_Running_Boards)."\r\n";
    } else {
        empty($Full_Running_Boards);
    }

    if($_POST['Additional_Clearance_Lights']) {
        $email_message .= "Additional Clearance Lights: ".clean_string($Additional_Clearance_Lights)."\r\n";
    } else {
        empty($Additional_Clearance_Lights);
    }

    if($_POST['Spare_Tire']) {
        $email_message .= "Spare Tire: ".clean_string($Spare_Tire)."\r\n";
    } else {
        empty($Spare_Tire);
    }

    if($_POST['Lined_Insulated_Dressing_Room']) {
        $email_message .= "Lined Insulated Dressing Room: ".clean_string($Lined_Insulated_Dressing_Room)."\r\n";
    } else {
        empty($Lined_Insulated_Dressing_Room);
    }

    if($_POST['lined_insulated_horse_area']) {
        $email_message .= "Lined Insulated Horse Area: ".clean_string($lined_insulated_horse_area)."\r\n";
    } else {
        empty($lined_insulated_horse_area);
    }

    if($_POST['Electric_Jack']) {
        $email_message .= "Electric Jack: ".clean_string($Electric_Jack)."\r\n";
    } else {
        empty($Electric_Jack);
    }

    if($_POST['Colored_Skin']) {
        $email_message .= "Colored Skin: ".clean_string($Colored_Skin)."\r\n";
    } else {
        empty($Colored_Skin);
    }

    if($_POST['Hay_Rack']) {
        $email_message .= "Hay Rack: ".clean_string($Hay_Rack)."\r\n";
    } else {
        empty($Hay_Rack);
    }

    if($_POST['Drop_Butt_Windows']) {
        $email_message .= "Drop Butt Windows: ".clean_string($Drop_Butt_Windows)."\r\n";
    } else {
        empty($Drop_Butt_Windows);
    }

    if($_POST['Saddle_Bosses']) {
        $email_message .= "Saddle Bosses: ".clean_string($Saddle_Bosses)."\r\n";
    } else {
        empty($Saddle_Bosses);
    }

    if($_POST['Additional_Bridle_Hooks']) {
        $email_message .= "Additional Bridle Hooks: ".clean_string($Additional_Bridle_Hooks)."\r\n";
    } else {
        empty($Additional_Bridle_Hooks);
    }

    if($_POST['Additional_Blanket_Bars']) {
        $email_message .= "Additional Blanket Bars: ".clean_string($Additional_Blanket_Bars)."\r\n";
    } else {
        empty($Additional_Blanket_Bars);
    }

    if($_POST['Foal_Divider']) {
        $email_message .= "Foal Divider: ".clean_string($Foal_Divider)."\r\n";
    } else {
        empty($Foal_Divider);
    }

    if($_POST['Polished_Nose']) {
        $email_message .= "Polished Nose: ".clean_string($Polished_Nose)."\r\n";
    } else {
        empty($Polished_Nose);
    }

    if($_POST['Feed_Bags']) {
        $email_message .= "Feed Bags: ".clean_string($Feed_Bags)."\r\n";
    } else {
        empty($Feed_Bags);
    }

    if($_POST['Hydraulic_Jack']) {
        $email_message .= "Hydraulic Jack: ".clean_string($Hydraulic_Jack)."\r\n";
    } else {
        empty($Hydraulic_Jack);
    }

    if($_POST['Extruded_Slat_Horse_Area']) {
        $email_message .= "Extruded Slat Horse Area: ".clean_string($Extruded_Slat_Horse_Area)."\r\n";
    } else {
        empty($Extruded_Slat_Horse_Area);
    }

    if($_POST['Bottom_Stall_Divider']) {
        $email_message .= "Bottom Stall Divider: ".clean_string($Bottom_Stall_Divider)."\r\n";
    } else {
        empty($Bottom_Stall_Divider);
    }

    if($_POST['Exterior_Package']) {
        $email_message .= "Exterior Package: ".clean_string($Exterior_Package)."\r\n";
    } else {
        empty($Exterior_Package);
    }

    if($_POST['Entertainment_Package']) {
        $email_message .= "Entertainment Package: ".clean_string($Entertainment_Package)."\r\n";
    } else {
        empty($Entertainment_Package);
    }

    if($_POST['Towing_Package']) {
        $email_message .= "Towing Package: ".clean_string($Towing_Package)."\r\n";
    } else {
        empty($Towing_Package);
    }

    if($_POST['Furnace']) {
        $email_message .= "Furnace: ".clean_string($Furnace)."\r\n";
    } else {
        empty($Furnace);
    }

    if($_POST['Microwave']) {
        $email_message .= "Microwave: ".clean_string($Microwave)."\r\n";
    } else {
        empty($Microwave);
    }

    if($_POST['Pass_Thru_Door']) {
        $email_message .= "Pass Thru Door: ".clean_string($Pass_Thru_Door)."\r\n";
    } else {
        empty($Pass_Thru_Door);
    }

    if($_POST['AC_13500_BTU_With_Heat_Strip']) {
        $email_message .= "AC 13500 BTU With Heat Strip: ".clean_string($AC_13500_BTU_With_Heat_Strip)."\r\n";
    } else {
        empty($AC_13500_BTU_With_Heat_Strip);
    }

    if($_POST['Bathroom_Skylight']) {
        $email_message .= "Bathroom Skylight: ".clean_string($Bathroom_Skylight)."\r\n";
    } else {
        empty($Bathroom_Skylight);
    }

    if($_POST['Bath_Sink']) {
        $email_message .= "Bath Sink: ".clean_string($Bath_Sink)."\r\n";
    } else {
        empty($Bath_Sink);
    }

    if($_POST['Horse_Area_Package']) {
        $email_message .= "Horse Area Package: ".clean_string($Horse_Area_Package)."\r\n";
    } else {
        empty($Horse_Area_Package);
    }

    if($_POST['Interior_Package']) {
        $email_message .= "Interior Package: ".clean_string($Interior_Package)."\r\n";
    } else {
        empty($Interior_Package);
    }

    if($_POST['Full_Length_Extruded_Slat']) {
        $email_message .= "Full Length Extruded Slat: ".clean_string($Full_Length_Extruded_Slat)."\r\n";
    } else {
        empty($Full_Length_Extruded_Slat);
    }

    if($_POST['Individual_Butt_Windows_30x26']) {
        $email_message .= "Individual Butt Windows 30x26: ".clean_string($Individual_Butt_Windows_30x26)."\r\n";
    } else {
        empty($Individual_Butt_Windows_30x26);
    }

    if($_POST['Front_Full_Riser_Wall']) {
        $email_message .= "Front Full Riser Wall: ".clean_string($Front_Full_Riser_Wall)."\r\n";
    } else {
        empty($Front_Full_Riser_Wall);
    }

    if($_POST['Dual_Hydraulic_Jacks_8_Wide']) {
        $email_message .= "Dual Hydraulic Jacks 8 Wide: ".clean_string($Dual_Hydraulic_Jacks_8_Wide)."\r\n";
    } else {
        empty($Dual_Hydraulic_Jacks_8_Wide);
    }

    if($_POST['Single_Leg_Hydraulic_Jack_6_9_Wide']) {
        $email_message .= "Single Leg Hydraulic Jack 6 9 Wide: ".clean_string($Single_Leg_Hydraulic_Jack_6_9_Wide)."\r\n";
    } else {
        empty($Single_Leg_Hydraulic_Jack_6_9_Wide);
    }

    if($_POST['Hickory_Interior_Package']) {
        $email_message .= "Hickory Interior Package: ".clean_string($Hickory_Interior_Package)."\r\n";
    } else {
        empty($Hickory_Interior_Package);
    }

    if($_POST['Nutmeg_Interior_Package']) {
        $email_message .= "Nutmeg Interior Package: ".clean_string($Nutmeg_Interior_Package)."\r\n";
    } else {
        empty($Nutmeg_Interior_Package);
    }

    //end of optional fields

    $email_message .= "Message: ".clean_string($comments)."\r\n";


    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    mail($email_to, $email_subject, $email_message, $headers);
    header("Location: $thankyou");
    ?>
    <script>location.replace('<?php echo $thankyou;?>')</script>
    <?php
    }
    die();
    ?>

        <?php

    $email_to = "random@something.com"; // your email address
    $email_subject = "Sales Lead"; // email subject line
    $thankyou = "/thank-you"; // thank you page

    // if you update the question on the form -
    // you need to update the questions answer below
    $antispam_answer = "8";

    ?>
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
jward
  • 1
  • 1
    remove irreverent information from your code – Muhammad Younas Jan 12 '16 at 14:11
  • 2
    Why are your email_to, email_subject & thankyou at the bottom of your script? They need to be above where they are going to be used otherwise php will spring chickens. Also have you checked the PHP & Mail server logs, perhaps a hint may be in them. – Tim Jan 12 '16 at 14:14

1 Answers1

0

a) You are setting $email_to, $email_subject and $thankyou AFTER you are trying to send the mail.

b) for bugfixing, have you tried echoing on the screen all of your variables? echo $email_message;

  • Where would you suggest to put the $email_to, $email_subject and $thankyou at. This is not a form I coded. All I did was add the if and else statements to the code. – jward Jan 12 '16 at 15:11
  • just copy those lines and move them above the email send function :) – fully stacked geek Jan 12 '16 at 15:18