2012-11-21 185 views
10

SMTP Error: Could not connect to SMTP host. Message could not be sent.無法連接到SMTP主機

Mailer Error: SMTP Error: Could not connect to SMTP host.

我似乎無法找到一種方法,使CentOS的下PHPMailer的工作。 Mail在XAMPP下的Windows下工作得很好,但是我總是在Linux下得到這個錯誤。

SMTP服務器是監聽端口25的Lotus Domino,CentOS機器根本沒有防火牆,奇怪的是即使郵件()也不起作用。它什麼也沒有返回(在Windows上返回1)。如果我通過CentOS服務器通過telnet發送郵件,它工作得很好,所以我不認爲這是一個網絡問題。它必須與PHP相關,但我不知道如何。

<?php 
require("class.phpmailer.php"); 
$mail = new PHPMailer(); 
$mail->IsSMTP(); 
$mail->Host = "192.168.x.x"; 
$mail->SMTPAuth = false; 
$mail->From = "[email protected]"; 
$mail->FromName = "XXX"; 
$mail->AddAddress("[email protected]"); 
$mail->IsHTML(true); 
$mail->Subject = "Test"; 
$mail->Body = "Test"; 
if(!$mail->Send()) 
{ 
    echo "Message could not be sent. <p>"; 
    echo "Mailer Error: " . $mail->ErrorInfo; 
    exit; 
} 
echo "Message has been sent"; 
?> 

只是爲了澄清上面的代碼工作在XAMPP(Windows)中。

我調試的PHPMailer的錯誤和錯誤發生在這裏(class.smtp.php方法連接()):

$this->smtp_conn = @fsockopen($host, // the host of the server 
          $port, // the port to use 
          $errno, // error number if any 
          $errstr, // error message if any 
          $tval); // give up after ? secs 
// verify we connected properly 
if(empty($this->smtp_conn)) { 
    $this->error = array("error" => "Failed to connect to server", 
         "errno" => $errno, 
         "errstr" => $errstr); 
    if($this->do_debug >= 1) { 
    echo "SMTP -> ERROR: " . $this->error["error"] . ": $errstr ($errno)" . $this->CRLF . '<br />'; 
    } 
    return false; 
} 

看起來它無法打開插座...

更新:使用$ mail-> SMTPDebug = 2;阿爾瓦羅的建議產生這樣的輸出:

SMTP -> ERROR: Failed to connect to server: Permission denied (13)

+2

您是否試過指定登錄憑據? –

+0

沒有。這是什麼意思?你的意思是把它們設置爲「」? – raz3r

+0

FYI tryed '$ mail-> SMTPAuth = true; $ mail-> Username =「」; $ mail-> Password =「」;' 剛纔,沒有運氣。 – raz3r

回答

10

可以實現與SMTPDebug財產調試模式,例如:

$mail = new PHPMailer(); 
// 1 = errors and messages 
// 2 = messages only 
$mail->SMTPDebug = 2; 

錯誤信息將被回顯到屏幕上。

更新:

使用fsockopen()一個權限被拒絕錯誤信息表明用戶PHP運行作爲不允許打開一個套接字。如果你仔細檢查過沒有防火牆,那可能是SELinux problem: - ?後一些RESERCH原來,SELinux的阻塞通信

SELinux的被激活,並且默認配置

+1

很好的抓住這裏,現在它給這個錯誤'SMTP - >錯誤:無法連接到服務器:權限被拒絕(13)'。它可以與Linux用戶的權限相關嗎? – raz3r

+2

你是一個天才,我使用了我在你的鏈接中找到的命令,它工作。所以IT畢竟是一個網絡相關的問題。那麼現在我知道如果我將來會有同樣的錯誤該怎麼辦!謝謝大家,感謝大家的幫忙;) – raz3r

30

OS的CentOS 6.3

無法發送電子郵件

。因爲這樣的SELinux不允許Apache(httpd,phpmailer)使用sendmail函數並進行任何類型的網絡連接。

使用getsebool命令,我們可以檢查是否允許httpd demon通過網絡建立連接併發送電子郵件。

getsebool httpd_can_sendmail 
getsebool httpd_can_network_connect 

該命令將返回一個布爾值或打開或關閉。如果它關閉,我們可以使用以下設置:

sudo setsebool -P httpd_can_sendmail 1 
sudo setsebool -P httpd_can_network_connect 1 

現在您可以測試您的php,查看SendMail是否正常工作的代碼。

+0

謝謝,解決了問題 – Piyuesh

+0

你今天是我的英雄。 –

+0

你先生救了我。我還想指出的是,在某些版本的CENTOS上,setsebool命令可能會運行一些慢,因此在運行命令時請耐心等待。 – JoeMoe1984

相關問題