2011-07-27 55 views
5

Possible Duplicate:
php mail() function on localhost幫助PHP mail()函數

我試圖做密碼恢復一些本地主機測試我的網站上,但是當我嘗試發送一封電子郵件,我得到以下錯誤:

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() 

以下是我的php.ini文件中的相關設置。

; For Win32 only. 
; http://php.net/smtp 
SMTP = localhost 
; http://php.net/smtp-port 
smtp_port = 25 

; For Win32 only. 
; http://php.net/sendmail-from 
sendmail_from = [email protected] 

我不確定將這些設置爲本地主機測試。我意識到我需要將SMTP設置爲我的提供商的郵件服務器,但我在共享的辦公樓工作,因此我不知道如何在這裏找到誰提供互聯網。

在此先感謝。

回答

2

PHP的郵件需要運行本地郵件服務器。

編輯:PHP文檔網站mail() reveils,你可以使用一個郵件包裹從PEAR

+0

...或者你可以使用ISP的一個。 – Shef

7

PHP的mail()函數不直接實現SMTP協議。相反,它依賴於sendmail()MTA(SMTP服務器),或像postfix或mstmp替換。 只要安裝了MTA,它就可以在Unix上正常工作。

在Windows(從PHP.net手冊):

The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).

所以 - 這個故事的寓意 - 你需要安裝郵件服務器。

但是 - 如果是爲了測試的目的只是 - 只是獲得實際實現了SMTP協議的PHP庫,並使用您的常規Gmail電子郵件地址發送電子郵件:使用PHP的郵件

相反的()使用其中之一:

  1. PHPMailer的
  2. SwiftMailer
  3. 的Zend \郵件

這些PHP庫真正實現SMTP協議,這樣可以輕鬆地從任何平臺上發送電子郵件,而無需安裝在同一臺機器上的郵件服務器:

PHPMailer的例子:

$mail->IsSMTP(); // telling the class to use SMTP 
$mail->Host  = "stmp.gmail.com"; // SMTP server 
$mail->SMTPDebug = 1;      // enables SMTP debug information (for testing) 
// 1 = errors and messages 
// 2 = messages only 
$mail->SMTPAuth = true;     // enable SMTP authentication 
$mail->SMTPSecure = "ssl";     // sets the prefix to the servier 
$mail->Host  = "smtp.gmail.com";  // sets GMAIL as the SMTP server 
$mail->Port  = 465;     // set the SMTP port for the GMAIL server 
$mail->Username = "[email protected]"; // GMAIL username 
$mail->Password = "pass111";   // GMAIL password 
$mail->SetFrom('[email protected]', 'My name is slim shady'); 
$mail->AddReplyTo("[email protected]","My name is slim shady"); 
$mail->Subject = "Hey, check out http://www.site.com"; 
$mail->AltBody = "Hey, check out this new post on www.site.com"; // optional, comment out and test 
$mail->MsgHTML($body); 
$address = "[email protected]"; 
$mail->AddAddress($address, "My name is slim shady");