2015-06-04 39 views
1

我想讓我的聯繫表單通過PHP發送郵件。我收到「HTTP ERROR 405.0 - 方法不允許」錯誤。 「您正在查找的頁面無法顯示,因爲正在使用無效的方法(HTTP動詞)。」用PHP控制HTML表格 - 錯誤

以下是我的HTML代碼。

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

這裏是我的PHP代碼 - (它是一個免費的PHP郵件發送者)

<?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; 
    } 
} 

?> 
+0

您描述的內容很可能不是由您發佈的任何代碼造成的。你的主機設置是什麼樣子? – castis

+0

我在我的本地盒子上運行這個。現在我已將其上傳到我的Azure Web服務器。這樣做後,我提到的錯誤已經消失,但現在當你點擊提交'formToEmail.php'頁面打開。它只是一個沒有任何內容,沒有電子郵件發送的白頁。我檢查了我的Azure設置,PHP版本5.4是安裝的。這是否需要更高版本? –

+0

[這可能對調試本地服務器有幫助](http://forums.iis.net/t/1154637.aspx?HTTP+Error+405+0+Method+Not+Allowed)。至於電子郵件問題,很難說,[檢查你的日誌],但如果我不得不猜測, ['(郵件()'需要一些額外的配置](http://stackoverflow.com/questions/10582492/how-can-i-send-an-email-using-php-at-windows-azure)。 – castis

回答

0

我知道這是愚蠢的,但只是檢查:多數認爲我有問題,該案件是因爲發件人的電子郵件沒有與我使用的域相同。是這樣嗎?