PHPMailer錯(cuò)誤SMTP Error: Could not connect to SMTP host的解決辦法

2013-07-17 14:14:37來(lái)源:西部e網(wǎng)作者:

PHPMailer是一個(gè)非常棒的開源郵件類,使用也非常簡(jiǎn)單,但是對(duì)于虛擬主機(jī)來(lái)說(shuō),往往要受到各種限制。剛才我在虛擬主機(jī)上使用PHPMailer就遇到一個(gè)“SMTP Error: Could not connect to SMTP host”錯(cuò)誤。下面介紹兩種解決辦法:

PHPMailer是一個(gè)非常棒的開源郵件類,使用也非常簡(jiǎn)單,但是對(duì)于虛擬主機(jī)來(lái)說(shuō),往往要受到各種限制。剛才我在虛擬主機(jī)上使用PHPMailer就遇到一個(gè)“SMTP Error: Could not connect to SMTP host”錯(cuò)誤。下面介紹兩種解決辦法:

這個(gè)錯(cuò)誤說(shuō)明虛擬主機(jī)不支持PHPMailer默認(rèn)調(diào)用的fsockopen函數(shù),找到class.smtp.php文件,搜索fsockopen,就找到了這樣一段代碼:

// connect to the smtp server
$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

方法1:將fsockopen函數(shù)替換成pfsockopen函數(shù)

因?yàn)閜fsockopen的參數(shù)與fsockopen基本一致,所以只需要將@fsockopen替換成@pfsockopen就可以了。

方法2:使用stream_socket_client函數(shù)

一般fsockopen()被禁,pfsockopen也有可能被禁,所以這里介紹另一個(gè)函數(shù)stream_socket_client()。

stream_socket_client的參數(shù)與fsockopen有所不同,所以代碼要修改為:

$this->smtp_conn = stream_socket_client("tcp://".$host.":".$port, $errno,  $errstr,  $tval);

這樣就可以了。

關(guān)鍵詞:PHPMailerSMTP

贊助商鏈接: