最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

phpmailer发送邮件报SMTP Error: Could not connect to SMTP host的解决方案办法

来源:http://www.erdangjiade.com/topic/628.html 你好,世界。 2017-09-25 22:42浏览(111)

之前做项目的时候做了一个用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

这样设置完,我的已经可以成功发送邮件了,如果同样有这方面问题的,可以参考上面的例子试一下。

以上就是phpmailer发送邮件报SMTP Error: Could not connect to SMTP host的解决办法的详细内容,更多请关注二当家的素材网其它相关文章!

评论0
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

1 2