Skip to main content
carries7564
Participating Frequently
August 24, 2017
Answered

.php form won't submit to email or redirect

  • August 24, 2017
  • 3 replies
  • 4690 views

Hello,

I have created an html form that is designed to do two things - submit form information to a specified email address and redirect users to another page on the site. I created a .php form to manage these actions. I got to a point where the redirect was successful, but the email was not. In attempting to fix this, the form now does neither...

I am completely new to web development, but would appreciate any insight you can provide. Thank you!

PHP

<?php

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

$to = 'example@example.com';

$subject = 'Whitepaper Request Form';

$email_message = 'Results from Whitepaper Request Form:\n\n';

$headers = 'From: example@example.com';

}

function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }

    $email_message .= "First Name: ".clean_string($_POST['First_Name'])."\n";

    $email_message .= "Last Name: ".clean_string($_POST['Last_Name'])."\n";

    $email_message .= "Address: ".clean_string($_POST['Address'])."\n";

    $email_message .= "City: ".clean_string($_POST['City'])."\n";

    $email_message .= "State: ".clean_string($_POST['State'])."\n";

    $email_message .= "Zip: ".clean_string($_POST['Zip'])."\n";

    $email_message .= "Phone: ".clean_string($_POST['Phone'])."\n";

    $email_message .= "Email: ".clean_string($_POST['Email'])."\n";

    $email_message .= "Company: ".clean_string($_POST['Company'])."\n";

$email_message .= "Title: ".clean_string($_POST['Title'])."\n";

mail($to,$subject,$email_message,$headers);

header('Location: ExampleRetro_WhtP.pdf');

?>

HTML

<section>

  <div class="row">

   

    <div class="col-lg-offset-3 col-lg-6 col-md-12"><form name="Whitepaper Request form" method="POST" action="Info-Request.php" class="text-center">

    <input name="First_Name" type="text" required class="contact-field-sm" id="fname" placeholder="First Name">

    <input name="Last_Name" type="text" required class="contact-field-sm" id="lname" placeholder="Last Name"><br><br>

    <input name="Address" type="text" class="contact-field-sm" id="address" placeholder="Address">

    <input name="City" type="text" class="contact-field-sm" id="city" placeholder="City"><br><br>

    <input name="State" type="text" class="contact-field-sm" id="state" placeholder="State">

    <input name="Zip" type="text" class="contact-field-sm" id="zip" placeholder="Zip"><br><br>

    <input name="Phone" type="tel" class="contact-field-sm" id="phone" placeholder="Phone">

    <input name="Email" type="email" class="contact-field-sm" id="email" placeholder="Email"><br><br>

    <input name="Company" type="text" required class="contact-field-sm" id="company" placeholder="Company">

    <input name="Title" type="text" required class="contact-field-sm" id="title" placeholder="Title"><br><br>

    <input id="submit" type="submit" name="submit" value="Submit">

    </form>

    This topic has been closed for replies.
    Correct answer Jon Fritz

    Thanks for your help. Yes, our host supports .php. I was trying a different code earlier today, which did get an email successfully through. It included recipient, subject, but didn't contain any of the form components... i.e., name, address, email, etc. Unfortunately, I have since attempted to fix the code, and as a result, been unsuccessful in getting the email through. Seems to me, though, the php mail function does work.


    Since you had a more basic form working previously (assuming the points osgood makes are taken care of and you are using the right email on the actual server, not a testing location), a common problem to look into would be the possibility that a spam filter, especially if your company has its email scrubbed by a third party before it's sent to you, is catching the multiple submissions as junk mail at the server level.

    3 replies

    carries7564
    Participating Frequently
    August 25, 2017

    Thanks everyone for your help! My site host confirmed that the php mail function is supported. However, the host did have outbound spam filtering in place that was preventing the successful transmittal of the email. I was exceeding a threshold of 5.5. Apparently "From:" should not include a real name, as it heightens the spam score. I changed it to a more "generic" name, and the email was processed.

    carries7564
    Participating Frequently
    August 24, 2017

    Yes, I have tested it on the actual server. I will clarify with my host whether or not the mail function is supported, and will look into the possibility of a spam filter. Thank you all for steering me in the right direction.

    Brainiac
    August 24, 2017

    Try moving the } after $headers = 'From: example@example.com'; to just before the closing ?> tag as below:

    <?php

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

    $to = 'example@example.com';

    $subject = 'Whitepaper Request Form';

    $email_message = 'Results from Whitepaper Request Form:\n\n';

    $headers = 'From: example@example.com';

    function clean_string($string) {

          $bad = array("content-type","bcc:","to:","cc:","href");

          return str_replace($bad,"",$string);

        }

        $email_message .= "First Name: ".clean_string($_POST['First_Name'])."\n";

        $email_message .= "Last Name: ".clean_string($_POST['Last_Name'])."\n";

        $email_message .= "Address: ".clean_string($_POST['Address'])."\n";

        $email_message .= "City: ".clean_string($_POST['City'])."\n";

        $email_message .= "State: ".clean_string($_POST['State'])."\n";

        $email_message .= "Zip: ".clean_string($_POST['Zip'])."\n";

        $email_message .= "Phone: ".clean_string($_POST['Phone'])."\n";

        $email_message .= "Email: ".clean_string($_POST['Email'])."\n";

        $email_message .= "Company: ".clean_string($_POST['Company'])."\n";

    $email_message .= "Title: ".clean_string($_POST['Title'])."\n";

    mail($to,$subject,$email_message,$headers);

    header('Location: ExampleRetro_WhtP.pdf');

    }

    ?>

    carries7564
    Participating Frequently
    August 24, 2017

    Thanks-- the .php form has successfully redirected! However, still no email has come through.

    Brainiac
    August 24, 2017

    Keep in mind that the suggestion of moving the } had no effect on the forum submission being able to redirect or send an email. There's something else happening off screen that forum users can not see. Since you haven't posted a link to the form it's a speculative guess on why your code is not working.

    I speculate that your server doesn't support mail function or it's not configured. Search online for smtp authentication for php and try that. While you're searching online do yourself and forum members a favor and search for php mail() not working to see why it's not working.


    EbaySeller  wrote

    Keep in mind that the suggestion of moving the } had no effect on the forum submission being able to redirect or send an email.

    Course it did, the php was invalid without it being in the correct place.

    [Mod note: Personal attack, albeit PG, removed. Come on guys, don't get this locked.]