用phpmailer發送郵件提示SMTP Error: Could not connect to SMTP host解決辦法


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

我擦,沒上線的時候好好的,次次成功,剛開始我以為是smtp地址的問題(我用的163郵箱),后來改成了QQ郵箱,發現還是沒有用,沒辦法,只好問度娘了,后來看着百度上的答案才明白除了google的smtp服務器收到請求"smtp"會接受,其他的服務器就像我用的163,QQ什么的必須要收到大寫的 "smtp"請求才行..........emmmmm.....

然后我在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

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

本文屬原創內容,為了尊重他人勞動,轉載請注明本文地址:

http://www.cnblogs.com/luokakale/p/7302373.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM