1

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 = 'myEmail@domain.com';//<== 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 = "myEmail@domain.com";//<== 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;
  }
}

?> 
Keith Prince
  • 33
  • 2
  • 9
  • What you've described is most likely not caused by any of the code you've posted. Whats your hosting setup look like? – castis Jun 04 '15 at 21:35
  • I was running this on my local box. Now I have uploaded it to my Azure Web Server. After doing this the error I mentioned has went away but now when you click Submit the 'formToEmail.php' page opens. Its just a white page with nothing on it and no email sends. I checked my Azure settings and PHP version 5.4 is what is installed. Does this need to be a higher version? – Keith Prince Jun 05 '15 at 01:05
  • [This might be helpful with debugging the local server thing](http://forums.iis.net/t/1154637.aspx?HTTP+Error+405+0+Method+Not+Allowed). As for the email problem, hard to tell, [check your logs](http://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log), but if I had to guess, [`mail()` needs some extra configuring](http://stackoverflow.com/questions/10582492/how-can-i-send-an-email-using-php-at-windows-azure). – castis Jun 05 '15 at 01:29
  • I have also tried upload this site to my GoDaddy Web Hosting, same issue happens. A blank page loads up. With GoDaddy they place an error on the page stating to contact a SysAdmin. I am supposed to have the php in a separate file, right? – Keith Prince Jun 05 '15 at 02:37

1 Answers1

0

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?

Clyff
  • 4,046
  • 2
  • 17
  • 32