Heim  >  Artikel  >  php教程  >  PHPMailer发送邮件”SMTP 错误:无法连接到 SMTP 主机“

PHPMailer发送邮件”SMTP 错误:无法连接到 SMTP 主机“

WBOY
WBOYOriginal
2016-06-08 17:22:011148Durchsuche

PHPMailer是一个邮件发送插件有很多朋友使用它来发邮件,但也有不少朋友在使用期PHPMailer发邮件时就碰到”SMTP 错误:无法连接到 SMTP 主机“错误了,出现这种问题我们从几个点来分享,一个是邮箱配置有问题,另一个是我们的php.ini环境中有些函数没开启导致的,下面我来给各位详细介绍一下问题的排除技巧。

<script>ec(2);</script>


原因分析

出现这个问题说明无法解析 SMTP 主机 的名称。

解决办法,这个要看邮箱支付不支持pop3发送与接收邮件这个可以邮件官方看看,如QQ邮件

phpmailer error SMTP Error: Could not connect to SMTP host Could not instantiate mail function

弄了半天,原来是不同邮件系统要求的smtp请求不同,但是都允许大写,有些不支持小写,比如网易,腾讯的邮箱。
原来的设置

$mail->SMTPAuth = true;
$mail->Mailer   = "smtp";
$mail->Host = "smtp.qq.com";
$mail->Port = 25; //设置邮件服务器的端口,默认为25
$mail->Username = "8515888@qq.com";
$mail->Password = "xxxxxxxxxx";

把smtp改成大写就可以了

$mail->Mailer   = "SMTP";

分析问题2,

还有大家就是使用了空间而不是服务器这样有可能像fsockopen、pfsockopen都禁用了,因为phpmailer需要使用fsockopen、pfsockopen才可以发邮件所以就会有问题了。

解决办法

找到class.smtp.php文件,大约在文件的128行吧,有这样一段代码:


// connect to the smtp server
    $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
方法1:将fsockopen函数替换成pfsockopen函数

因为pfsockopen的参数与fsockopen基本一致,所以只需要将@fsockopen替换成@pfsockopen就可以了。

方法2:使用stream_socket_client函数

一般fsockopen()被禁,pfsockopen也有可能被禁,所以这里介绍另一个函数stream_socket_client()。

stream_socket_client的参数与fsockopen有所不同,所以代码要修改为:

$this->smtp_conn = stream_socket_client("tcp://".$host.":".$port, $errno,  $errstr,  $tval);

这样就可以了。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn