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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17672960/
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-25 16:14:25  来源:igfitidea点击:

PHPMailer error: SMTP -> ERROR: Failed to connect to server

phpsmtpphpmailer

提问by FrancescoMussi

I try to google all the morning and i think i need Stackoverflow now!

我整个早上都在尝试谷歌,我想我现在需要 Stackoverflow!

I wrote a simple script to send a mail (from hotmail to gmail) but i get this error:

我编写了一个简单的脚本来发送邮件(从 hotmail 到 gmail),但出现此错误:

SMTP -> ERROR: Failed to connect to server: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (10060)SMTP Connect() failed. Error

SMTP -> ERROR: Failed to connect to server: 连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机没有响应。(10060)SMTP Connect() 失败。错误

This is the code:

这是代码:

<?php
require_once("../includes/phpMailer/class.phpMailer.php");
require_once("../includes/phpMailer/class.smtp.php");


$to_name = "RECEIVER NAME";
$to = "[email protected]";


$subject = "Mail test at " . strftime("%T", time());

$message = "This is a test message";
$message = wordwrap($message, 70);


$from_name = "MY NAME";
$from = "[email protected]";


$mail = new PHPMailer();

$mail->IsSMTP();
$mail->SMTPDebug  = 2;
$mail->Host = "smtp.live.com";
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "MY USERNAME (hotmail)";
$mail->Password = "MY PASSWORD (hotmail)";


$mail->FromName = $from_name;
$mail->From = $from;
$mail->AddAddress($to, $to_name);
$mail->Subject = $subject;
$mail->Body = $message;

$result = $mail->Send();

echo $result ? 'Sent' : 'Error';

?>

Another info is that not even the standard mail() function worked, and checking php info i found this:

另一个信息是,即使是标准的 mail() 函数也没有工作,检查 php 信息我发现了这个:

sendmail_from - MY PROPER MAIL (hotmail)

sendmail_from - 我的正确邮件 (hotmail)

sendmail_path - no value

sendmail_path - 没有价值

SMTP - localhost

SMTP - 本地主机

smtp_port - 25

smtp_port - 25

Thank you!!

谢谢!!

回答by mti2935

I believe port 25 is blocked on smtp.live.com. I cannot connect to smtp.live.com:25 from here either. Try using port 587 instead, with TLS. So, it would be:

我相信 smtp.live.com 上的端口 25 被阻止了。我也无法从这里连接到 smtp.live.com:25。尝试使用端口 587 和 TLS。所以,它会是:

$mail->Port = 587;
$mail->SMTPSecure = 'tls';   

回答by Sohail xIN3N

I found a solution for this problem, try this

我找到了解决这个问题的方法,试试这个

Check whether your PHP is using openSSLextension or not...!

检查您的 PHP 是否正在使用openSSL扩展...!

  1. Edit your php.inifrom your installed php folder
  2. Search for extension=php_openssl.dll
  3. The initial will look like this ;extension=php_openssl.dll
  4. Remove the ';' and it will look like this extension=php_openssl.dll
  5. If you can't find the extension=php_openssl.dll, add this line extension=php_openssl.dll.
  6. Then restart your Xampp or LAMP or APACHE server (depends upon which of these you're using).
  1. 从您安装的 php 文件夹中编辑您的php.ini
  2. 搜索extension=php_openssl.dll
  3. 初始看起来像这样;extension=php_openssl.dll
  4. 去除那个 ';' 它看起来像这个extension=php_openssl.dll
  5. 如果找不到extension=php_openssl.dll,请添加这一行extension=php_openssl.dll
  6. 然后重新启动您的 Xampp 或 LAMP 或 APACHE 服务器(取决于您使用的是哪一个)。

Hope this method shall solve your problem...

希望这个方法能解决你的问题...

回答by CodesmithX

You may want to check the supported ports. For instance, my host supports smtp via ports 25, 3535 and 80.
Using port 80 worked for me

您可能需要检查支持的端口。例如,我的主机通过端口 25、3535 和 80 支持 smtp。
使用端口 80 对我有用