简体   繁体   English

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

[英]SMTP Error: Could not connect to SMTP host using PHPMailer

we are using SMTP advanced authentication to send a test mail using PHPMailer. 我们正在使用SMTP高级身份验证来使用PHPMailer发送测试邮件。 We are using 1and1.com server with SMTP and SSL for E-mail Exchange. 我们正在将1and1.com服务器与SMTP和SSL一起用于电子邮件交换。 We need to run this php page from a third party server. 我们需要从第三方服务器运行此php页面。 we have taken a example from the downloaded PHPMailer package. 我们以下载的PHPMailer软件包中的示例为例。 we have tried with "test_pop_before_smtp_advanced" example and " SMTP advanced test with authentication" example. 我们尝试使用“ test_pop_before_smtp_advanced”示例和“带有身份验证的SMTP高级测试”示例。 In both cases we are getting same Error. 在这两种情况下,我们都会得到相同的错误。

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   = "xxx@abc.com"; // SMTP account username
  $mail->Password   = "xxxxx";        // SMTP account password
  $mail->AddReplyTo('mno@abc.com', 'First Last');
  $mail->AddAddress('mno@abc.com', 'John Doe');
  $mail->SetFrom('xxx@abc.com', '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... 谢谢...

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 = 'xxxxx@xxx.com';  // 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. 我必须将SMTPSecutre设置为tls或完全删除此行,将smtp.1and1.net的ip用于主机(您可以通过ping或whois进行验证),并将端口设置为25。即使1and1建议使用其他端口的频率更高。 Hopefully this saves someone some time. 希望这可以节省一些时间。

PHP suddenly changed in version 5.6 to be much more strict about SSL connections. PHP在5.6版中突然更改为对SSL连接更加严格。 This causes many web contact forms to break, often without any error message being displayed. 这会导致许多Web联系人表单中断,通常不会显示任何错误消息。 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: 如果要继续使用自签名证书或允许非SSL(不安全)连接,则可以使用PHPMailer的快速解决方法:

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

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. 由于您无法连接,请花点时间检查您的登录详细信息是否正确,即使是我们中最好的人也可能会误输入密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM