phpmailer发邮件提示SMTP->ERROR: Failed to connect to server的解决方法
使用phpmailer插件发邮件失败提示:SMTP -> ERROR: Failed to connect to server: Connection timed out (110) The following From address failed: [email protected] Mailer Error: The following From address failed: [email protected]
连接服务器失败:连接超时(110)
在本地(windows)测试的时候一切正常,一发到服务器(centos)上就提示以上错误了。本地正常说明用户名,密码都没错,设置也没错,是服务器的设置问题。到网上找了好久,都没找到可行的方法。
我是用gmail的服务,包括在iptables中添加465端口,关闭iptables,关闭selinux 都不行。测试了php函数fopen,fsockopen,pfscockopen都正常。期间还使用163邮箱测了下,同样提示以上错误。
后面查啊查,查到了一个贴子,http://drupal.org/node/805834 看13楼,大意是说可能是系统开启了ipv6,而php至少在v5.3.2版本前,在处理ipv6上有些已知的bug......如果是这种情况,可以把smtp的服务器直接设置成ipv4地址.....
所以来排查下。(如果你赶时间的话,直接拖到下文解决方法设置上试下吧。)
1.看下是否开启了ipv6
$ ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:16:3B:E8:F5:26
inet addr:216.24.198.11 Bcast:216.24.198.127 Mask:255.255.255.128
inet6 addr: fe80::216:3eff:fee8:f226/64 Scope:Link
inet6 addr: 2604:6600:5::5769:54ef/64 Scope:Global
看inet6 addr说明开启了支持ipv6
2.连接下gmail的smtp看是否能连接上,gmail的smtp的端口是 465
2.1 telnet 下
$telnet smtp.gmail.com 465
提示找不到telnet命令。
2.2 查看下是否安装了telnet
$rpm -qa|grep telnet
2.3 什么都没提示,说明没有安装。telnet有服务端和客户端,缺少哪个安装哪个。直接yum。
#yum -y install telnet
安装服务端
#yum -y install telnet-server
2.4 安装成功后编辑/etc/xinetd.d/telnet
# vi /etc/xinetd.d/telnet
把
disable = yes
改成
disable = no
2.5 开启xinetd
#service xinetd start
2.6 测试连接本地
$ telnet localhost
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
..
连接正常
2.7 连接gmail 的smtp 端口是 465
$ telnet smtp.gmail.com 465
Trying 2607:f8b0:400e:c00::6d...
有类似这么一串2607:f8b0:400e:c00::6d 的话说明连的是ipv6,一直卡着连不上。
2.8 ping 下gmail的smtp,找到它的ipv4
$ ping smtp.gmail.com
PING gmail-smtp-msa.l.google.com (74.125.141.108) 56(84) bytes of data.
64 bytes from da-in-f108.1e100.net (74.125.141.108): icmp_seq=1 ttl=50 time=33.5 ms
74.125.141.108 就是它的ipv4地址
2.9 telnet 到ipv4上看下
$ telnet 74.125.141.108 465
Trying 74.125.141.108...
Connected to 74.125.141.108.
Escape character is '^]'.
出现以上这些表示可连接上。看来是默认连接到ipv6的地址上了。
3.解决方法
其中一个方法是禁用服务器的ipv6。其实更好的方法是在发送邮件的代码里把smtp的地址设置为ipv4,如下:
$mail->Host = "173.194.79.108"; // sets GMAIL as the SMTP server smtp.gmail.com
然后我测试发送了一下,成功!
所以,提示Failed to connect to server: Connection timed out (110)
The following From address failed邮件发不了的问题之一可能是因为ipv6连接不上导致的。也跟php没什么关系,不是php的bug,因为我们这连接测试用talnet连接的,一样优先连接到的是ipv6,不是php中使用代码连的。
4.telnet 不太安全,所以测完把它给禁掉,以后有需要再开启
#vi /etc/xinetd.d/telnet
把
disable = no
改成
disable = yes
停止服务
# service xinetd stop
5.附上gmail中使用phpmailer发邮件的配置
<?php include("mailsender/class.phpmailer.php"); include("mailsender/class.smtp.php"); // note, this is optional - gets called from main class if not already loaded $mail = new PHPMailer(); $mail->CharSet = 'gbk'; $mail->SMTPDebug = 1; //is debug $mail->IsSMTP(); $mail->SMTPAuth = true; // enable SMTP authentication $mail->SMTPSecure = "ssl"; // sets the prefix to the servier $mail->Host = "173.194.79.108"; // sets GMAIL as the SMTP server smtp.gmail.com $mail->Port = 465; // set the SMTP port $mail->Username = "[email protected]"; // GMAIL username $mail->Password = "密码"; // GMAIL passwordfds_E%$432blog_ $mail->From = "[email protected]"; $mail->FromName = "[email protected]";//$shorttitle世纪预言的部落格 $mail->Subject = "世纪预言的测试标题"; $mail->AltBody = "31232132121"; //Text Body $mail->WordWrap = 50; // set word wrap $mail->MsgHTML("31232132121邮件测test试邮件测test试邮件测test试邮件测test试1232132131"); $mail->AddReplyTo("[email protected]","$title");//回复地址(邮箱,姓名) $mail->AddAddress("[email protected]","收件人姓名");//收件箱 可能被认为垃圾邮件 $mail->IsHTML(true); // send as HTML if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message has been sent".date('Y-m-d H:m:s'); } ?>
类别:OperationMaintenance 阅读(0) 评论(0) 发表时间:2013-05-05 20:38 星期日