首页 > Web开发 > 详细

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

时间:2019-08-01 13:50:25      阅读:83      评论:0      收藏:0      [点我收藏+]

转自:https://www.cnblogs.com/raincowl/p/8875647.html

//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上试下,一运行,出现

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都没什么问题,可是依然发送报上述的错误,后来查看官网资料,发现下面一段代码

$mail->SMTPOptions = array(
ssl => array(
    verify_peer => false,
    verify_peer_name => false,
    allow_self_signed => true
    )
);

将这段代码加上之后,邮件发送成功。

 

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

原文:https://www.cnblogs.com/rxbook/p/11281697.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!