服务器发送邮件提示Could not connect to SMTP host的解决方法
发送邮件提示“SMTP Error Could not connect to SMTP host”主要有2种情况,要么是主机商禁用了SMTP功能,要么是关闭了fsockopen函数,下面看SJY分别来做讲解。
禁用了SMTP功能
既然禁用了SMTP功能,只好用MAIlSend来替代SMTP了,方法是打开phpmailer目录下的class.phpmailer.php文件,大概在363行找到如下代码
public function IsSMTP() {
$this->Mailer = 'smtp';
}
把小写的smtp改成大写的SMTP
这个地方的修改不是使用了smtp来发送邮件,而是使用了另外一种方式发送邮件,在class.phpmailer.php文件大概572行,可以找到以下代码
Switch($this->Mailer) {
case 'sendmail':
return $this->SendmailSend($header, $body);
case 'smtp':
return $this->SmtpSend($header, $body);
default:
return $this->MailSend($header, $body);
}
从这段代码可以看出,由于SMTP和smtp不相等 所以选择的是下面MailSend发送邮件 并不是使用smtp发送邮件。实际上上面的smtp改成任意名称,都会执行默认的 MailSend
禁用了fsockopen函数
在主机商禁用mail()函数的时候,我们只能通过SMTP插件来发送邮件,而SMTP插件则是通过PHPmailer连接远程SMTP服务器来发送邮件,如果服务器禁用了fsockopen()函数就会出现错误 Could not connect to SMTP host
打开PHPmailer目录中的class.smtp.php文件,WordPress程序则打开wp-includes/class.smtp.php 文件 搜索 fsockopen 应该只有一个结果,替换为 pfsockopen
如果主机商同时禁用了pfsockopen函数,那么就用 stream_socket_client 下面是我替换的代码,供大家参考
$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
替换为
$this->smtp_conn = @stream_socket_client("tcp://".$host.":".$port, $errno, $errstr, $tval);
您可能感兴趣的文章
声明:本文来自互联网或用户投稿,该文观点仅代表作者本人,不代表本站立场。文章及其配图仅供学习和交流之用,版权归原作者所有,如有内容侵权或者其他违规问题,请联系本站处理。



![服务器LNMP环境 [emerg] open()错误问题的解决方法](/d/file/uploads/2025/0711/175220848897481876.jpg)