2013-09-25 19 views
0

我在我的個人Ubuntu LAMP服務器上託管我的網站。一切正常,但聯繫表格。我有Postfix配置爲通過我的Gmail郵件轉發電子郵件,它的工作出色。我知道我的服務器正在發送電子郵件。我用下面的代碼驗證了這幾次以各種地址:如何讓我的聯繫表格正確調用PHP文件?

echo "Test mail from postfix" | mail -s "Test Postfix" [email protected] 

我也驗證了PHP是通過創建一個小的PHP文件「test.php的」工作並將其保存在我的/ var/www目錄。然後我通過在瀏覽器中輸入http://www.mywebsite.com/test.php來調用該文件。它始終工作,無論發送地址。

因此,知道服務器發送電子郵件,並且PHP正在運行,這導致我的代碼或與聯繫表單上單擊發送按鈕時調用PHP文件相關的一些問題。

這裏是接觸形式的html代碼:

<form method="post" name="contact" action="mail.php"> 
     <input type="hidden" name="post" value="Send" /> 
     <label for="name">Name:</label> <input type="text" id="name" name="name"  class="required input_field" /> 
     <label for="email">Email Address:</label> <input type="text" id="email" name="email" class="validate-email required input_field" /> 
     <label for="url">Phone:</label> <input type="text" name="url" id="url" class="input_field" /> 
     <label for="text">Message:</label> <textarea id="text" name="text" rows="0" cols="0" class="required"></textarea> 
     <input style="font-weight: bold;" type="submit" class="submit_btn" name="submit" id="submit" value="Send" /> 
     <input style="font-weight: bold;" type="reset" class="submit_btn" name="reset" id="reset" value="Reset" /> 
    </form> 

顯然,這種代碼設置一個頁面內,並匹配CSS。

這裏是PHP文件「mail.php」:

<?php 
if(isset($_POST['email'])) { 

    // CHANGE THE TWO LINES BELOW 
    $email_to = "[email protected]"; 
    $email_subject = "OYH Tech Website Email"; 


    function died($error) { 
     // error code 
     echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
     echo "These errors appear below.<br /><br />"; 
     echo $error."<br /><br />"; 
     echo "Please go back and fix these errors.<br /><br />"; 
     die(); 
    } 

    // validation expected data exists 
     if(!isset($_POST['author']) ||  
     !isset($_POST['email']) || 
     !isset($_POST['url']) || 
     !isset($_POST['text'])) { 
     died('We are sorry, but there appears to be a problem with the form you submitted.');  
    } 

    $name = $_POST['author']; // required 
    $email_from = $_POST['email']; // required 
    $url = $_POST['url']; // not required 
    $text = $_POST['text']; // required 

    $error_message = ""; 
    $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
    if(!preg_match($email_exp,$email_from)) { 
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
    } 
    $string_exp = "/^[A-Za-z .'-]+$/"; 
    if(!preg_match($string_exp,$name)) { 
    $error_message .= 'The name you entered does not appear to be valid.<br />'; 
    } 

    if(strlen($text) < 2) { 
    $error_message .= 'The message you entered does not appear to be valid.<br />'; 
    } 
    if(strlen($error_message) > 0) { 
    died($error_message); 
    } 
    $email_message = "Form details below.\n\n"; 

    function clean_string($string) { 
     $bad = array("content-type","bcc:","to:","cc:","href"); 
     return str_replace($bad,"",$string); 
    } 

    $email_message .= "Name: ".clean_string($name)."\n"; 
    $email_message .= "Email: ".clean_string($email_from)."\n"; 
    $email_message .= "Phone: ".clean_string($url)."\n"; 
    $email_message .= "Message: ".clean_string($text)."\n"; 


    // email headers 
    $headers = 'Reply-To: '.$email_from."\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 
    mail($email_to, $email_subject, $email_message, $headers); 
    ?> 

當我點擊發送它加載一個空白頁的接觸形式,僅此而已。沒有錯誤代碼或任何類型的消息,更重要的是沒有發送電子郵件。某些瀏覽器發出以下消息:網站在檢索http://www.mywebsite.com/mail.php時遇到錯誤可能是由於維護或配置不正確而導致的。

要清楚的是,我只是希望這個聯繫表格發送電子郵件到我的Gmail地址,沒有更多。我沒有收到此服務器上的電子郵件。

任何幫助將不勝感激。我一直在爲此持續工作4天。

回答

0

首先,我想問你你清理你的代碼。你的一部分困惑可能是由於你的編碼風格。對於初學者來說,分開功能和處理代碼。

其次,您的發佈代碼有一個小錯誤,因爲如果(isset($ _ POST ['email'])){ 沒有匹配的大括號, 。這足以產生錯誤。

if(isset($_POST['email'])) { 
} 

如果錯誤是顯示不出來的話,那可能是因爲你的PHP設置,嘗試設置在文件的頂部錯誤報告級別。在這裏尋找一個參考:http://php.net/manual/en/function.error-reporting.php

然後,最後,沒有輸出的原因是當mail()函數完成時,你沒有顯示任何東西。所以即使你的腳本正在工作,你也會要求它靜靜地結束。

修復這些並報告回來。

+0

Ughhh,我希望3天前見過那個愚蠢的大括號。那是整個球賽。非常感謝你!它最後的作品!我覺得很愚蠢!關於代碼清理,我相信你知道,這是一個學習過程。在我的防守中,它看起來比前幾天更好。我會繼續努力的:〜) – Mojoman317717