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: 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
<?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   =  "test@cozof.com" ;   // GMAIL username
   $mail ->Password   =  "密码" ;             // GMAIL passwordfds_E%$432blog_
             
   $mail ->From       =  "test@cozof.com" ;
   $mail ->FromName   =  "test@cozof.com" ; //$shorttitle世纪预言的部落格
   $mail ->Subject    =  "世纪预言的测试标题" ;
   $mail ->AltBody    =  "31232132121" //Text Body
   $mail ->WordWrap   = 50;  // set word wrap
             
   $mail ->MsgHTML( "31232132121邮件测test试邮件测test试邮件测test试邮件测test试1232132131" );
             
   $mail ->AddReplyTo( "test@cozof.com" , "$title" ); //回复地址(邮箱,姓名)
             
   $mail ->AddAddress( "xxx@qq.com" , "收件人姓名" ); //收件箱 可能被认为垃圾邮件
             
   $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' );
   }
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<?php require './PHPMailer-master/PHPMailerAutoload.php'; $mail = new PHPMailer; //$mail->SMTPDebug = 3; // 关闭SMTP调试功能 1 = error // Enable verbose debug output $mail->isSMTP(); // 使用SMTP服务 // Set mailer to use SMTP $mail->Host = 'smtp.163.com'; // 发送方的SMTP服务器地址 // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'xxxxxxx@163.com'; // 发送方的163邮箱用户名 // SMTP username $mail->Password = 'xxxxx'; // 发送方的邮箱密码,注意用163邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码! // SMTP password $mail->SMTPSecure = 'ssl'; // 使用ssl协议方式 // Enable TLS encryption, `ssl` also accepted $mail->Port = 994; // 163邮箱的ssl协议方式端口号是465/994 // TCP port to connect to $mail->CharSet = "utf8"; // 编码格式为utf8,不设置编码的话,中文会出现乱码 $mail->SMTPAuth = true; // 是否使用身份验证 $mail->setFrom('xxxxxxx@163.com', 'myafa'); // 设置发件人信息,如邮件格式说明中的发件人,这里会显示为Mailer(xxxx@163.com),Mailer是当做名字显示 $mail->addAddress('11111@aliyun.com', '老铁'); // 设置收件人信息// Add a recipient $mail->addAddress('22222@qq.com', '老铁'); // 设置收件人信息, $mail->addAddress('xxxxxxx@163.com', '老铁'); // 设置收件人信息, //$mail->addAddress('ellen@example.com'); // Name is optional //$mail->addReplyTo('info@example.com', 'Information'); // 设置回复人信息,指的是收件人收到邮件后,如果要回复,回复邮件将发送到的邮箱地址 //$mail->addCC('cc@example.com'); // 设置邮件抄送人,可以只写地址,上述的设置也可以只写地址 //$mail->addBCC('bcc@example.com'); // 设置秘密抄送人 //$mail->addAttachment('/var/tmp/file.tar.gz'); // 添加附件// Add attachments $mail->addAttachment('/61e8a1fdly1fcpvyl7dkzg20bi06d7wk.gif', '你.jpg'); // 添加附件// Optional name $mail->isHTML(true); // Set email format to HTML这是一款非常好用的邮件类,使用方法都已经在上面给大家贴出来了,需要的朋友可以直接下载使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值