Question

The problem I'm having is that I want to pass through data from a number of fields in a form into one email send via PHP, the concerned code is as follows:

$mailcheck = spamcheck($_POST['inputemail']);
        if ($mailcheck==FALSE)
          {
          echo "Invalid input";
          }
        else
          {
          // This still needs to be debugged online as soon as possible
          $from = $_POST['inputemail'];
          $name = $_POST['inputname'];
          $phone = $_POST['inputphone'];
          $date = $_POST['inputdate'];
          $time = $_POST['inputtime'];
          $data = $_POST['inputprotection'];
          $subject = $_POST['inputdropdown'];
          $message = $_POST['inputmessage'];
          $message = wordwrap($message, 70);
          mail("UP498701@myport.ac.uk",$subject,$message,"From: $from\n" . "Name: $name\n" . "Phone: $phone\n" . "Date: $date\n" . "Time: $time\n" . "Keep Data? $data\n");
          echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
          }
        }

This is where I'm declaring all the information to be passed as a header for the email. The current PHP passes syntax check as works to the extent that an email is sent containing the $to, $subject, $message all fine, but the $header only passes through the final part (Keep Data? $data\n). When I remove all the other fields and simply keep the $data part, the email stops sending any $header. I also have an issue with the redirect, which has been removed from the below code and will be inserted as soon the current issue is resolved. The current full PHP is:

<html>
<body>

    <?php
    function spamcheck($field)
      {
      // Sanitize e-mail address
      $field=filter_var($field, FILTER_SANITIZE_EMAIL);
      // Validate e-mail address
      if(filter_var($field, FILTER_VALIDATE_EMAIL))
        {
        return TRUE;
        }
      else
        {
        return FALSE;
        }
      }
    ?>

    <?php 
    if (isset($_POST['inputemail']))
        {
        $mailcheck = spamcheck($_POST['inputemail']);
        if ($mailcheck==FALSE)
          {
          echo "Invalid input";
          }
        else
          {
          // This still needs to be debugged online as soon as possible
          $from = $_POST['inputemail'];
          $name = $_POST['inputname'];
          $phone = $_POST['inputphone'];
          $date = $_POST['inputdate'];
          $time = $_POST['inputtime'];
          $data = $_POST['inputprotection'];
          $subject = $_POST['inputdropdown'];
          $message = $_POST['inputmessage'];
          $message = wordwrap($message, 70);
          mail("UP498701@myport.ac.uk",$subject,$message,"From: $from\n" . "Name: $name\n" . "Phone: $phone\n" . "Date: $date\n" . "Time: $time\n" . "Keep Data? $data\n");
          echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
          }
        }
    ?>

</body>

and is temporarily hosted at http://luke-hale.com/temp/contact.html. I'm sure this is just a case of being misinformed on my part, but any help would be great, cheers!

EDIT:

okay so that issue is sorted with:

          $subject = $_POST['inputdropdown'];
          $message = $_POST['inputmessage'] . "\r\n\r\n" . $_POST['inputname'];
          $message = wordwrap($message, 70);
          $headers = "From: " . $_POST['inputemail'];
          mail("UP498701@myport.ac.uk",$subject,$message,$headers);
          echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";

Which works though doesn't display the $headers anywhere, but I'm not too fussed about this, the next thing is the redirect, which I would usually run through via:

header("refresh:5;url=http://www.domain.com")

Though this was not working correctly earlier, I will apply an edit when tested for anyones future reference.

EDIT

So the form works but the re-direct does not. The site is still hosted on the same domain, but now the full PHP looks like this:

<html>
<body>

    <?php
    function spamcheck($field)
      {
      // Sanitize e-mail address
      $field=filter_var($field, FILTER_SANITIZE_EMAIL);
      // Validate e-mail address
      if(filter_var($field, FILTER_VALIDATE_EMAIL))
        {
        return TRUE;
        }
      else
        {
        return FALSE;
        }
      }
    ?>

    <?php 
    if (isset($_POST['inputemail']))
        {
        $mailcheck = spamcheck($_POST['inputemail']);
        if ($mailcheck==FALSE)
          {
          echo "Invalid input";
          }
        else
          {
          $subject = $_POST['inputdropdown'];
          $message = "Name: " . $_POST['inputname'] . "\r\n\r\n" . "Email: " . $_POST['inputemail'] . "\r\n\r\n" . "Phone: " . $_POST['inputphone'] . "\r\n\r\n" . "Date: " . $_POST['inputdate'] . "\r\n\r\n" . "Time: " . $_POST['inputtime'] . "\r\n\r\n" . "Retain Data? " . $_POST['inputprotection'] . "\r\n\r\n" . "Message: " . $_POST['inputmessage'];
          $message = wordwrap($message, 70);
          $headers = "From: " . $_POST['inputemail'];
          mail("UP498701@myport.ac.uk",$subject,$message,$headers);
          echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
          }
        }
    header("refresh:5;url=http://www.domain.com")
    ?>

</body>

The form still sends fine but the return states that the line beginning "header" cannot be passed because the browser data has already been modified. I'm not sure sure what I've done or where so if anyone could lend a hand, that'd be great!

FINAL EDIT

My final, fully working, code:

<?php
function spamcheck($field)
{
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
<?php
ob_start();
if (isset($_REQUEST['inputemail'])&&($_REQUEST['inputname'])&&($_REQUEST['inputmessage']))
{
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid email, you'll be redirected ";
header("refresh:5;url=http://www.luke-hale.com/");
}
else
{
$subject = $_POST['inputdropdown'];
$message = "Name: " . $_POST['inputname'] . "\r\n\r\n" . "Email: " . $_POST['inputemail'] . "\r\n\r\n" . "Phone: " . $_POST['inputphone'] . "\r\n\r\n" . "Date: " . $_POST['inputdate'] . "\r\n\r\n" . "Time: " . $_POST['inputtime'] . "\r\n\r\n" . "Retain Data? " . $_POST['inputprotection'] . "\r\n\r\n" . "Message: " . $_POST['inputmessage'];
$message = wordwrap($message, 70);
$headers = "From: " . $_POST['inputemail'];
mail("UP498701@myport.ac.uk",$subject,$message,$headers);
echo "Thank you for your messgae, I'll get back to you as soon as possible! You'll be redirected in 5 seconds.";
}
header("refresh:5;url=http://www.luke-hale.com/");
}
else
{
echo "You did not fill all the required fields, please try again.";
header("refresh:5;url=http://www.luke-hale.com/");
}
?>
Was it helpful?

Solution

Try putting your form data into the $message variable, insert \n after each attribute to give a new line. Do not use the header.

OTHER TIPS

Location and Refresh both require an absolute URI

header('Location: http://www.domain.com');

Examples for mail:
https://stackoverflow.com/a/22833890/3489793
https://stackoverflow.com/a/22831825/3489793

mail("Your Email Address Here",$subject,$therest,"subject: $subject\n");   

U said this: "The mail function is also not the issue, and correct syntax is" mail($to,$subject,$message,$headers,$parameters)

Explanation:

your email adress = $to  
$subject = $subject  
$therest = $message (rename it if you want)  
"subject: $subject\n" = the header (change it to $headers if you want that)

Header example (as in the documentation)

$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

You could echo a Javascript at the bottom of your PHP script which redirects instead, like this:

$url = "url";
$time = 5000 // time in milliseconds

print "<script>window.setTimeout(function(){window.location='".$url."'},".$time.")       </script>"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top