php SMTP 错误:无法使用 PHPMailer 连接到 SMTP 主机

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

SMTP Error: Could not connect to SMTP host using PHPMailer

phpemailphpmailer

提问by Bharath

we are using SMTP advanced authentication to send a test mail using PHPMailer. We are using 1and1.com server with SMTP and SSL for E-mail Exchange. We need to run this php page from a third party server. we have taken a example from the downloaded PHPMailer package. we have tried with "test_pop_before_smtp_advanced" example and " SMTP advanced test with authentication" example. In both cases we are getting same Error.

我们正在使用 SMTP 高级身份验证来使用 PHPMailer 发送测试邮件。我们使用带有 SMTP 和 SSL 的 1and1.com 服务器进行电子邮件交换。我们需要从第三方服务器运行这个 php 页面。我们从下载的 PHPMailer 包中举了一个例子。我们已经尝试过“test_pop_before_smtp_advanced”示例和“SMTP高级测试与身份验证”示例。在这两种情况下,我们都会遇到相同的错误。

SMTP Error: Could not connect to SMTP host.

SMTP 错误:无法连接到 SMTP 主机。

Here is the php file we have written for sending Mail.

这是我们为发送邮件而编写的 php 文件。

<html>
<head>
<title>PHPMailer - SMTP advanced test with authentication</title>
</head>
<body>

<?php
echo "hai";
include('class.phpmailer.php');
include('class.smtp.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch


$mail->SMTPSecure = "ssl";
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";
echo "hai1";
$mail->IsSMTP(); // telling the class to use SMTP

try {

  $mail->Host       = "smtp.1and1.com"; // SMTP server
  $mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->Host       = "smtp.1and1.com"; // sets the SMTP server
  $mail->Port       = 587;                    // set the SMTP port for the GMAIL server
  $mail->Username   = "[email protected]"; // SMTP account username
  $mail->Password   = "xxxxx";        // SMTP account password
  $mail->AddReplyTo('[email protected]', 'First Last');
  $mail->AddAddress('[email protected]', 'John Doe');
  $mail->SetFrom('[email protected]', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->Body = 'hello';

 if(!$mail->Send()) {
   echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
   echo "Message sent!";
}
} catch (phpmailerException $e) {
echo "error";
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo "err";
  echo $e->getMessage(); //Boring error messages from anything else!
}
?>

</body>
</html>

Can anyone help us in this regard, please suggest any other easy approach for the same purpose. Thank you...

任何人都可以在这方面帮助我们,请为相同的目的建议任何其他简单的方法。谢谢...

回答by Dakota Hipp

I had the same problem, and was walked through a bunch of different port, domain, and security configuration attempts. Try to use these settings, I found them on another help site and they worked for me.

我遇到了同样的问题,并且经历了一系列不同的端口、域和安全配置尝试。尝试使用这些设置,我在另一个帮助站点上找到了它们,它们对我有用。

$mail->IsSMTP();                                    // Set mailer to use SMTP
$mail->Host = '74.208.5.2';             // Specify main and backup SMTP server (server name)
$mail->Port = 25;                // TCP port to connect to 587 or 465, 425, 25 

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

$mail->SMTPAuth = true;       // Enable SMTP authentication
$mail->SMTPSecure = 'tls';     // Enable TLS encryption, `ssl` also accepted

I had to set SMTPSecutre to tls or remove this line completely, use smtp.1and1.net's ip for the host (you can verify with a ping or whois), and set port to 25. Even though 1and1 suggested other ports more often. Hopefully this saves someone some time.

我必须将 SMTPSecutre 设置为 tls 或完全删除此行,使用 smtp.1and1.net 的主机 ip(您可以使用 ping 或 whois 进行验证),并将端口设置为 25。即使 1and1 更频繁地建议其他端口。希望这可以节省一些时间。

回答by Steven Greenwaters

PHP suddenly changed in version 5.6 to be much more strict about SSL connections. This causes many web contact forms to break, often without any error message being displayed. If you want to continue using a self-signed certificate, or allowing a non-SSL (insecure) connection, a quick workaround for PHPMailer is to use:

PHP 在 5.6 版中突然更改为对 SSL 连接更加严格。这会导致许多 Web 联系表单中断,通常不会显示任何错误消息。如果您想继续使用自签名证书,或允许非 SSL(不安全)连接,PHPMailer 的快速解决方法是使用:

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

回答by d.abyss

if you are using an SSL connection - try changing your port to default SSL port 465

如果您使用 SSL 连接 - 尝试将您的端口更改为默认 SSL 端口 465

If you are not using SSL, try removing these lines:

如果您没有使用 SSL,请尝试删除这些行:

$mail->SMTPSecure = "ssl";
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";

and change the port to default port 25.

并将端口更改为默认端口 25。

As you are getting a could not connect, take a moment to check your login details are correct, even the best of us can fall foul of a miss-typed password.

当您遇到无法连接时,请花点时间检查您的登录详细信息是否正确,即使是我们中最优秀的人也可能会因输入错误而犯规。