首頁  >  文章  >  後端開發  >  phpmailer發送郵件封包SMTP Error: Could not connect to SMTP host的解決辦法

phpmailer發送郵件封包SMTP Error: Could not connect to SMTP host的解決辦法

一个新手
一个新手原創
2017-09-13 09:30:454614瀏覽

之前做專案的時候做了一個用phpmailer發送郵件的功能《CI框架結合PHPmailer發送郵件》,昨天步署上線(剛開始用新浪雲,嫌貴,換成阿里了),測試的時候,發送郵件卻意外報錯了..........

phpmailer發送郵件封包SMTP Error: Could not connect to SMTP host的解決辦法

然後我在class.phpmailer.php中,將


#
1 public function IsSMTP() {
2     $this->Mailer = 'smtp';
3   }
4 
5 //改成
6 public function IsSMTP() {
7     $this->Mailer = 'SMTP';
8   }

然後將:


 1 switch($this->Mailer) { 
 2         case 'sendmail': 
 3           return $this->SendmailSend($header, $body); 
 4         case 'smtp': 
 5           return $this->SmtpSend($header, $body); 
 6         default: 
 7           return $this->MailSend($header, $body); 
 8       } 
 9 
 10 
 11 //改成
 12 switch($this->Mailer) {
 13         case 'sendmail':
 14           return $this->SendmailSend($header, $body);
 15         case 'SMTP':
 16           return $this->SmtpSend($header, $body);
 17         default:
 18           return $this->MailSend($header, $body);
 19       }

我本來以為這樣就可以了,重啟Apache,再次測試一下,結果第一個錯誤是解決了,又出現了一個報錯:

Could not instantiate mail function

? ? ? ? ?

不知道你們有沒有出現,我運氣差,只好又求助度娘,終於找到原因:有的虛擬主機,或伺服器,為了安全起見屏蔽了「fsockopen()函數」導致無法傳送郵件。

下面說一下解決方法:

首先,在php.ini中去掉下面的兩個分號

;extension=php_sockets.dll

#;extension=php_openssl.dll

之前我用PHPmailer的時候已經去掉了,這裡只是提示一下。

 接著取代fsockopen函數

將class.smtp.php檔案中fsockopen函數換成pfsockopen函數:


 1 $this->smtp_conn = @fsockopen($host,    // the host of the server 
 2                                                    $port,    // the port to use 
 3                                  $errno,   // error number if any 
 4                                  $errstr,  // error message if any 
 5                                  $tval);   // give up after ? secs 
 6  
 7  
 8 //fsockopen改为: 
 9 $this->smtp_conn = @pfsockopen($host,    // the host of the server
 10                                  $port,    // the port to use
 11                                  $errno,   // error number if any
 12                                  $errstr,  // error message if any
 13                                  $tval);   // give up after ? secs

這樣設定完,我的已經可以成功發送郵件了,如果同樣有這方面問題的,可以參考上面的例子試試看。


#

以上是phpmailer發送郵件封包SMTP Error: Could not connect to SMTP host的解決辦法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn