• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

.php form won't submit to email or redirect

Community Beginner ,
Aug 24, 2017 Aug 24, 2017

Copy link to clipboard

Copied

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>

Views

4.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 24, 2017 Aug 24, 2017

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.

Votes

Translate

Translate
LEGEND ,
Aug 24, 2017 Aug 24, 2017

Copy link to clipboard

Copied

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');

}

?>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 24, 2017 Aug 24, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 24, 2017 Aug 24, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 24, 2017 Aug 24, 2017

Copy link to clipboard

Copied

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.]

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 24, 2017 Aug 24, 2017

Copy link to clipboard

Copied

osgood_  wrote

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.

The php was invalid without it being in the correct place.

The php was not invalid with it being in the place it was.

Here is the original code with the } in the place you said to move it:

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';

}

That code is basically saying : if you clicked the submit button then assign values to the variables. The location redirect code will still work even if someone didn't submit the form and instead simply visited the php page. It would still redirect the page, and since the OP mentioned that submitting the form did not send an email it's not the placement of the } that is the cause.

[Mod note: Personal attacks removed.]

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

EbaySeller  wrote

and since the OP mentioned that submitting the form did not send an email it's not the placement of the } that is the cause.

[Mod note: Personal attacks removed.]

As it stood the OP had no chance of even clicking the sumbit button, the page would redirect immediately on visiting the page. To make the form work correctly all the code needs to be wrapped in  {     }

The script can only send mail, then redirect correctly if the code is formatted to do so hence the } was originally in the wrong place

Youre just trying to save face for a rather idiotic suggestion that the OP wants the page to redirect without the form being completed, which quite clearly they don't.

Go learn some simple php instead of whatever it is you do, selling ice-creams probably....

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 24, 2017 Aug 24, 2017

Copy link to clipboard

Copied

carries7564  wrote

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

The form works perfectly. I suspect your server does not support the php mail function. Can you check with your host...?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 24, 2017 Aug 24, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 24, 2017 Aug 24, 2017

Copy link to clipboard

Copied

carries7564  wrote

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.

It might well support php but have you specifically asked if they support the 'mail' function. Some hosts wont allow it to be used.

I've tested the form on my server which does support the 'mail' function and all the information comes through to the recipients email address

I assume you did change the email address in the script to that where you want the email to be sent:

$to = 'example@example.com';

Plus you are testing this on the server where the form is going to be hosted and not locally...?

You do have an error in your code the line below should be as below without the n\n that's unlikely to stop all the mail coming through though...

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 24, 2017 Aug 24, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 24, 2017 Aug 24, 2017

Copy link to clipboard

Copied

Just because your server supports PHP code does NOT mean they have turned on the Php Mail () function.   That has to be configured on the server as well.  Can you find out?

Nancy O'Shea— Product User, Community Expert & Moderator

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 24, 2017 Aug 24, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines