phpmailer的SMTP ERROR: Failed to connect to server: 10


请问,我在win7上学习使用phpmailer时,出现这种错误怎么处理啊?

SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. SMTP connect() failed.
我php用的是apache,win7防火墙关了,用的邮箱也开启smtp功能了


smtp没连接上,或者你的用户名密码错误
追问
用户名密码没错啊?我开过了smtp
我也检查了allow_url_fopen = On
extension=php_openssl.dll等等。都还是一样。
服务器也重启了。
请问还有其他可能原因么?
我的邮箱是刚刚申请的,phpmailer会不会暂时不能用?
追答
pop3 和 smtp都设置正确了吗

原文链接

使用phpmailer插件发邮件失败提示:SMTP -> ERROR: Failed to connect to server: Connection timed out (110) The following From address failed: test@cozof.com Mailer Error: The following From address failed: test@cozof.com

连接服务器失败:连接超时(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,如下:

1
$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发邮件的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

0

阅读 评论 收藏 转载 喜欢 打印举报
已投稿到:
加载中,请稍候......
评论加载中,请稍候...
发评论

登录名:密码:找回密码 注册 记住登录状态

昵   称:

评论并转载此博文sg_trans.gif

发评论

以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

  

新浪BLOG意见反馈留言板 不良信息反馈 电话:4006900000 提示音后按1键(按当地市话标准计费) 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

新浪公司 版权所有

原文地址:https://www.cnblogs.com/lxwphp/p/15455156.html