php PHPMailer SMTP 错误:无法连接到服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36351817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:47:18  来源:igfitidea点击:

PHPMailer SMTP ERROR: Failed to connect to server

phpphpmailer

提问by Gavin

<?php

include("class.phpmailer.php");
include("class.smtp.php");

$mail = new PHPMailer();

$mail->IsSMTP();  // telling the class to use SMTP
$mail->Mailer = "smtp";
$mail->SMTPDebug = 2;
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->Port = 587; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls";

$mail->Username = "[email protected]"; // SMTP username
$mail->Password = "password"; // SMTP password 

$mail->From = "[email protected]";
$mail->FromName = "Webmaster";

$mail->AddAddress("[email protected]");
$mail->AddReplyTo("[email protected]", "Webmaster");
$mail->IsHTML(true);

$mail->Subject  = "First PHPMailer Message";
$mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;

if(!$mail->Send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}
?>

It returns an error

它返回一个错误

2016-04-01 08:41:43 SMTP ERROR: Failed to connect to server: (0) 
2016-04-01 08:41:43 SMTP connect() failed. 

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message was not sent.Mailer error: SMTP connect() failed. 

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

I am hosting this php on my xampp local server. The extension=php_openssl.dllon php.iniis already uncommended.

我在我的 xampp 本地服务器上托管这个 php。在extension=php_openssl.dllphp.ini已经uncommended。

采纳答案by Atilla Arda A??kg?z

Your configuration might be wrong. I believe that if you change your host into smtp.gmail.comit might solve your problem.

您的配置可能有误。我相信,如果您将主机更改为smtp.gmail.com它可能会解决您的问题。

I noticed that you set security tls but you want to connect with ssl as well.

我注意到您设置了安全 tls,但您也想与 ssl 连接。

Change $mail->Host = "ssl://smtp.gmail.com";into $mail->Host = "smtp.gmail.com";and security to ssl.

更改$mail->Host = "ssl://smtp.gmail.com";$mail->Host = "smtp.gmail.com";ssl 并将安全性更改为ssl。

From this answer:

这个答案

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "[email protected]";
$mail->Password = "password";
$mail->SetFrom("[email protected]");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("[email protected]");

 if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
    echo "Message has been sent";
 }