HTML Control Form with PHP - Errors

98 Views Asked by At

I am trying to get my Contact Form to send an email via PHP. I am receiving an error of "HTTP ERROR 405.0 - Method Not Allowed". "The page you are looking for cannot be displayed because an invalid method (HTTP verb) is being used."

Below is my HTML code.

<div class="col-md-9 col-xs-12 forma">
    <form action="formToEmail.php" name="myemailform" method="POST">
       <input type="text" class="col-md-6 col-xs-12 name" name='name' placeholder='Name *'/>
       <input type="text" class="col-md-6 col-xs-12 Email" name='email' placeholder='Email *'/>
       <textarea type="text" class="col-md-12 col-xs-12 Message" name='message' placeholder='Message *'></textarea>
       <div class="cBtn col-xs-12">
          <input type="submit" name="submit" value="Send" />
             <ul>
                <li class="clear"><a href="#"><i class="fa fa-times"></i>clear form</a></li>
                <li class="send"><a href="#"><i class="fa fa-share"></i>Send Message</a></li>               
             </ul>
       </div>
    </form>     
</div>

And here is my PHP code - (Its a free PHP email sender)

<?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'];
$message = $_POST['message'];

//Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

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

$email_from = '[email protected]';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
    "Here is the message:\n $message"

$to = "[email protected]";//<== update the email address
$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: thank-you.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;
  }
}

?> 
1

There are 1 best solutions below

0
On

I know it's silly, but just checking: most of the cases that i had problem with that was because the sender email didn't had the same domain i was using. Is that the case?