服务器发送邮件出现Could not connect to SMTP host

来自:互联网
时间:2018-08-09
阅读:

发送邮件提示  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);

如有不明白的,就参考 fsockopen函数被禁用的解决方案

返回顶部
顶部