0

<form action="form-to-email.php" method="post">
<p><label for='name'> Name: </label><br/><input type="text" name="Name" /></p>
<p><label for='Subject'> Subject: </label> <br/><input type="text" name="Subject" /></p>
<p><label for='email'> E-Mail: </label> <br/><input type="text" name="email" /></p>
<p><label for='message'>Message:</label><br/><textarea name='message' cols="40" rows="8">
</textarea></p>
<input type="submit" name='submit' class="submit" value="submit"/> <br/>
</form>
<?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['Name'];
$visitor_email = $_POST['email'];
$Subject = $_POST['Subject'];
$message = $_POST['message'];


if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}

$email_from = 'email@website.com';
$email_subject = "New Form submission:\n $Subject";
$email_body = "You have received a new message from the user $name.\n".
    "Here is the message:\n $message".

$to = "businessemail@email.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thankyou.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
{
    return true;
  }
  else
    {
    return false;
  }
}

?>

The php is a copy/paste job and used to work. But I recently found out that it isn't working now. I'm using godaddy for hosting which is using cPanel Hosting.

Ben
  • 8,696
  • 7
  • 37
  • 72
  • What are you doing to prevent this page from being used by a robot to send SPAM? – Brian A. Henning Dec 11 '15 at 16:10
  • 1
    `isn't working` isn't a good description of the issue. Are you getting an error? Email just isn't sending? Etc... – chris85 Dec 11 '15 at 16:12
  • It redirects to the "thank you" page. But im not getting any errors (even with a blank form) but the email isnt sending – perfectjake Dec 11 '15 at 16:13
  • Put the `mail` in an `if`. You've checked your spam folder as well? – chris85 Dec 11 '15 at 16:16
  • Ah, thanks. It's been going to spam folder, formatting is off but Gmail was flagging it as spam. Silly me! Formatting is off on it though. but I'll figure that one out – perfectjake Dec 11 '15 at 16:19

0 Answers0