2010-09-16 242 views
2

我有一個腳本從網站上的表單發送電子郵件。我知道代碼很好 - 我以前曾用兩家不同的託管公司進行過測試。這次我在一個實際的服務器上使用它,而不是託管公司。基本上用戶填寫表單,點擊提交併獲取消息已發送的確認頁面。唯一的不是電子郵件。下面是該腳本:PHP電子郵件腳本不發送電子郵件

$nickname = $_REQUEST['nickname'] ; 
    $email = $_REQUEST['email'] ; 
    $tel = $_REQUEST['tel'] ; 
     $comp = $_REQUEST['comp'] ; 
    $message = $_REQUEST['message'] ; 


// Let's check if all fields are filled in! 
if(empty($nickname) || empty($email) || empty($comp)) 
{ 
$error = "All of the required fields have not been completed, <a href=\"javascript:history.go(-1)\">please click here to go back.</a>"; 
} 
else 
{ 
$content= "$nickname has sent you an e-mail from XXX 
Query: 
$message 
You can contact $nickname via Email: $email. <br />Other relevant details of individual: <br />Telephone Number: $tel <br />Company: $comp"; 

mail("[email protected]", "Query", $content, "From: $email"); //first thing has to be address it is going to, then what the subject of the mail should be, the content and a from address which will be the query submitter. 
echo "<h2>$nickname</h2><br></br> 
    Your query has been succesfully sent. <br></br><br></br> 
    We will deal with this query and be in touch as soon as possible.<br></br><br></br><br></br> 
    The contact details you submitted are: <br></br><br></br> 
    <strong>Email:</strong>&nbsp; $email<br></br><br></br> 
    <strong>Phone:</strong>&nbsp; $tel<br></br><br></br> 
    <strong>Company:</strong>&nbsp; $comp<br></br><br></br> 
    <a //href=\"javascript:history.go(-1)\"> Click here to return to the contact page.</a></br>"; 
}; 

?> 

我不知道這是否事項要麼但直到最近,PHP不會在服務器上運行,並給了一個HTTP錯誤405錯誤,說動詞是不允許的。這已經解決了。

+0

如果代碼適用於其他網站託管服務很好,我會建議你看看SMTP或郵寄發送服務設置。嘗試做一個簡單的郵件()沒有其他邏輯或代碼作爲一個理智的測試。 – Extrakun 2010-09-16 12:49:01

+0

我有幾個有關您的電子郵件問題的問題。你的服務器上是否安裝了Parellels Plesk?你有一臺SMTP服務器在服務器上運行和設置嗎?您的服務器是基於Linux還是Windows?這是一個Linux安裝程序:http://articles.sitepoint.com/article/advanced-email-php這裏是一個Windows安裝程序:http://www.geeklog.net/faqman/index.php?op=view&t= 19 – 2010-09-16 12:51:42

+0

我對服務器不太熟悉。我沒有設置這個。我探討了一下,並且我在IIS管理器中看到SMTP虛擬服務器已停止,所以我想知道這是否是我的問題? – 109221793 2010-09-16 12:57:19

回答

3

很有可能您的服務器沒有安裝MTA(郵件傳輸代理)。 PHP的mail()函數不會自己發送電子郵件,它會將結果傳遞給系統的默認MTA。

快速問題 - 您使用的是基於Windows的服務器嗎? Unix/Linux服務器在其默認設置(通常是sendmail或exim或其他)中幾乎總是有MTA,但Windows服務器通常不會;你必須自己安裝一個。

3

PHP腳本甚至不會發送電子郵件,像sendmail,qmail或postfix這樣的郵件服務器。檢查它們是否配置良好。

2

作爲Elzo已經表示,郵件功能依賴於郵件服務器在您的服務器上正確設置和配置。看看http://www.php.net/manual/en/book.mail.php 的配置選項,用於在php中設置郵件服務器訪問。

mail()本身只是底層功能的包裝。

如果您沒有管理員權限運行時配置變量可以是非常有用的:

<?php 
ini_set("SMTP","smtp.example.com"); 
ini_set('sendmail_from', '[email protected]'); 
?> 

的sendmail_path變量需要的系統配置文件中設置這樣的:

sendmail_path = /usr/sbin/sendmail -t -i 

如果你想要更好的錯誤報告和支持異常信息的消息,當一些事情出錯時,你應該看看phpmailer http://phpmailer.worxware.com/

它爲您提供了一個相當不錯的面向對象的接口,可以發送包含良好和翔實錯誤報告的電子郵件。