Php错误邮件提示linux,linux下phpmailer发送邮件出现SMTP ERROR: Failed to connect to server: (0)错误...

在作项目的过程当中,后期客户提出了发送邮件的需求,既然客户有需求,那么没啥说的,上呗。php

通过网上的通常资料查找,PHPMailer这个插件貌似用起来不错,那就从github clone一份下来,下载连接是PHPMailer。html

官当demo以下:固然相关的配置要换成你本身的linux

//Create a new PHPMailer instance

$mail = new PHPMailer;

//Tell PHPMailer to use SMTP

$mail->isSMTP();

//Enable SMTP debugging

// 0 = off (for production use)

// 1 = client messages

// 2 = client and server messages

$mail->SMTPDebug = 2;

//Set the hostname of the mail server

$mail->Host = 'smtp.qq.com';

//$mail->Host = 'smtp.163.com';

//Set the SMTP port number - likely to be 25, 465 or 587

$mail->Port = 465;

$mail->SMTPSecure = "ssl";

//$mail->SMTPAuth = false;

//$mail->SMTPSecure = false;

//Whether to use SMTP authentication

$mail->SMTPAuth = true;

//Username to use for SMTP authentication

$mail->Username = 'xxxxxxx@qq.com';

//$mail->Username = 'raincowl@163.com';

//Password to use for SMTP authentication

//$mail->Password = 'lingshuan008';

$mail->Password = 'password';

//Set who the message is to be sent from

//$mail->setFrom('raincowl@163.com', 'fromuser');

$mail->setFrom('xxxxxxx@qq.com', 'fromuser');

//Set an alternative reply-to address

//$mail->addReplyTo('raincowl@163.com', 'First Last');

$mail->addReplyTo('xxxxxxx@qq.com', 'First Last');

//Set who the message is to be sent to

$mail->addAddress('yyyyyyy@qq.com', 'John Doe');

//Set the subject line

$mail->Subject = 'PHPMailer SMTP test';

//Read an HTML message body from an external file, convert referenced images to embedded,

//convert HTML into a basic plain-text alternative body

//$mail->msgHTML(file_get_contents('contents.html'), __DIR__);

$mail->msgHTML('hello,body!');

//Replace the plain text body with one created manually

$mail->AltBody = 'This is a plain-text message body';

//Attach an image file

//$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors

if (!$mail->send()) {

echo 'Mailer Error: ' . $mail->ErrorInfo;

} else {

echo 'Message sent!';

}

首先本地windows下,发送成功,很happy,那就放上linux上试下,一运行,出现git

SMTP ERROR: Failed to connect to server: (0)

SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

而后各大技术论坛查资料;首先检查linux服务器上的openssl和sockets已经开启,fsockopen函数也没有禁用,邮箱的smtp服务也已经开启,依然仍是不行。至于有一种说法将smtp换成SMTP,是可以发送成功,后来查看下源码,发现这种只是经过sendmail发送的,不是smtp。而后,直接在linux上ping smtp.qq.com,telnet  smtp.qq.com 465都没什么问题,但是依然发送报上述的错误,后来查看官网资料,发现下面一段代码github

$mail->SMTPOptions = array(

'ssl' => array(

'verify_peer' => false,

'verify_peer_name' => false,

'allow_self_signed' => true

)

);

就猜想了是否是本身的ssl认证没有经过,将这段代码加上以后,神奇的事情发生了,邮件发送成功。windows

到此,问题圆满解决。服务器

若有疑问,能够留言。app

转载请注明出处!socket

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值