【问题标题】:PHPMailer: SMTP Error: Could not connect to SMTP hostPHPMailer:SMTP 错误:无法连接到 SMTP 主机
【发布时间】:2011-03-29 12:16:54
【问题描述】:

我在几个项目中使用过 PHPMailer,但现在我被困住了。它给了我错误:
SMTP 错误:无法连接到 SMTP 主机。
我已经尝试从 Thunderbird 发送电子邮件并且它有效!但不是通过 PHPMailer ... 这是 Thunderbird 的设置:

服务器名称:mail.exampleserver.com
端口:587
用户名:user@exampleserver.com
安全认证:否
连接安全:STARTTLS

我已经将这些与我上次使用 PHPMailer 的项目中的服务器进行了比较,它们是:

服务器名称:mail.exampleserver2.com
端口:465
用户名:user@exampleserver2.com
安全认证:否
连接安全:SSL/TLS

我的php代码是:

 $mail = new PHPMailer();
 $mail->IsSMTP(); // send via SMTP
 $mail->Host = SMTP_HOST; // SMTP servers
 $mail->Port = SMTP_PORT; // SMTP servers
 $mail->SMTPAuth = true; // turn on SMTP authentication
 $mail->Username = SMTP_USER; // SMTP username
 $mail->Password = SMTP_PASSWORD; // SMTP password
 $mail->From = MAIL_SYSTEM;
 $mail->FromName = MAIL_SYSTEM_NAME;
 $mail->AddAddress($aSecuredGetRequest['email']);
 $mail->IsHTML(true); // send as HTML

我哪里错了?

【问题讨论】:

标签: php email phpmailer


【解决方案1】:

由于这个问题在 google 中出现的频率很高,我想在这里分享一下我针对 PHP 刚刚升级到 5.6 版(具有更严格的 SSL 行为)的情况的解决方案。

PHPMailer wiki 对此有一个部分:

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#php-56-certificate-verification-failure

建议的解决方法包括以下代码:

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

这应该适用于 PHPMailer 5.2.10(及更高版本)。

注意:显然,正如该 wiki 中所建议的,这应该是一个临时解决方案!

解决此问题的正确方法是用一个好的证书替换无效、配置错误或自签名的证书。

【讨论】:

  • 对于那些在 localhost 邮件中苦苦挣扎并面临自签名问题的人来说,这是最好的答案。
  • 同意!这是开发环境中的最佳解决方案。只需将该代码包装在 if 语句中即可检查环境类型。如果是开发环境,请使用该代码,否则让服务器担心认证。
  • 最佳解决方案,经过数小时的各种 PHPMailer 代码参数更改破坏我的大脑后,它最终成为一个错误或与较新的 PHP 版本不兼容。我现在在本地开发环境中使用 localhost 发送。干得好貂!
  • 非常感谢!这两天一直在寻找解决方案。
  • 旁注:我的 LetsEncrypt 证书在 Postfix 中设置错误。必须是fullchain.pemprivkey.pem
【解决方案2】:

在我的例子中,是 PHP 中缺少 SSL 支持导致了这个错误。

所以我启用了 extension=php_openssl.dll

$mail->SMTPDebug = 1; 也暗示了这个解决方案。

2017 年更新

$mail->SMTPDebug = 2;,见:https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#enabling-debug-output

【讨论】:

  • 启用这个 PHP 扩展(WAMP 菜单选项)是我所需要的。谢谢。
  • 你是我的英雄。几个小时以来,我一直在为本地主机上的电子邮件苦苦挣扎,但这解决了我所有的问题!
  • 你是如何启用这个扩展的? @wessel
  • 如何在 GoDaddy 主机中启用 extension=php_openssl.dll?谢谢
【解决方案3】:

你的问题很可能是这个

连接安全性:STARTTLS 连接安全:SSL/TLS

这是 2 种不同的协议,您使用的是正确的一种吗,无论您在 Thunderbird 中使用的哪种协议都需要使用。

尝试设置变量:

// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';

【讨论】:

    【解决方案4】:

    我遇到了同样的问题,这是因为 PHPMailer 意识到服务器支持 STARTTLS,所以它试图自动将连接升级为加密连接。我的邮件服务器与我网络中的网络服务器位于同一个子网上,这些网络服务器都位于我们的域防火墙之后,所以我不太担心使用加密(而且生成的电子邮件无论如何都不包含敏感数据)。

    所以我继续做的是在 class.phpmailer.php 文件中将 SMTPAutoTLS 更改为 false。

    /**
     * Whether to enable TLS encryption automatically if a server supports it,
     * even if `SMTPSecure` is not set to 'tls'.
     * Be aware that in PHP >= 5.6 this requires that the server's certificates are valid.
     * @var boolean
     */
    public $SMTPAutoTLS = false;
    

    【讨论】:

    • 谢谢。您不需要编辑基类来更改它,只需执行 $mail->SMTPAutoTLS = false;
    【解决方案5】:

    我遇到了类似的问题,发现是 php.ini 中的 openssl.cafile 配置指令需要设置为允许验证安全对等方。您只需将其设置为证书颁发机构文件的位置,就像您可以在 http://curl.haxx.se/docs/caextract.html 获得的文件一样。

    这个指令是 PHP 5.6 的新指令,所以当我从 PHP 5.5 升级时这让我措手不及。

    【讨论】:

    • 我遇到了同样的问题,如果我在 phpmailer 电子邮件中将对等验证设置为 false,则使用 PHP5.6 进入垃圾邮件文件夹,知道如何设置证书路径吗?在哪里获得证书?在 Debian 8 VPS 中
    • 谢谢。对我来说,这已经奏效了。即: cd /etc/php5 && wget curl.haxx.se/ca/cacert.pem && echo "openssl.cafile = /etc/php5/cacert.pem" >> /etc/php5/apache2/php.ini
    【解决方案6】:

    以下代码对我有用:

    $mail = new PHPMailer(true);
    
    $mail->isSMTP();// Set mailer to use SMTP
    $mail->CharSet = "utf-8";// set charset to utf8
    $mail->SMTPAuth = true;// Enable SMTP authentication
    $mail->SMTPSecure = 'tls';// Enable TLS encryption, `ssl` also accepted
    
    $mail->Host = 'smtp.gmail.com';// Specify main and backup SMTP servers
    $mail->Port = 587;// TCP port to connect to
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
    $mail->isHTML(true);// Set email format to HTML
    
    $mail->Username = 'Sender Email';// SMTP username
    $mail->Password = 'Sender Email Password';// SMTP password
    
    $mail->setFrom('example@mail.com', 'John Smith');//Your application NAME and EMAIL
    $mail->Subject = 'Test';//Message subject
    $mail->MsgHTML('HTML code');// Message body
    $mail->addAddress('User Email', 'User Name');// Target email
    
    
    $mail->send();
    

    【讨论】:

    • 我很怀疑,但这让它起作用了。我认为 Gmail 需要 SMTPOptions 设置,因为我没有它就无法工作。
    【解决方案7】:

    mail.exampleserver.com 存在吗??? ,如果没有试试下面的代码(你必须有gmail帐户)

    $mail->SMTPSecure = "ssl";  
    $mail->Host='smtp.gmail.com';  
    $mail->Port='465';   
    $mail->Username   = 'you@gmail.com'; // SMTP account username
    $mail->Password   = 'your gmail password';  
    $mail->SMTPKeepAlive = true;  
    $mail->Mailer = "smtp"; 
    $mail->IsSMTP(); // telling the class to use SMTP  
    $mail->SMTPAuth   = true;                  // enable SMTP authentication  
    $mail->CharSet = 'utf-8';  
    $mail->SMTPDebug  = 0;   
    

    【讨论】:

      【解决方案8】:
      $mail->SMTPDebug = 2; // to see exactly what's the issue
      

      就我而言,这有帮助:

      $mail->SMTPSecure = false;
      $mail->SMTPAutoTLS = false;
      

      【讨论】:

        【解决方案9】:

        由于这是一个常见错误,请查看PHPMailer Wiki 进行故障排除。

        这对我也有用

        $mailer->Port = '587';
        

        【讨论】:

          【解决方案10】:

          我遇到了类似的问题。我安装了不准备管理 SSL 连接的 PHPMailer 1.72 版。升级到最新版本解决了这个问题。

          【讨论】:

            【解决方案11】:

            我最近处理了这个问题,问题的原因原来是我连接的 SMTP 服务器上的根证书是Sectigo root certificate that recently expired

            如果您通过 SSL/TLS 或 STARTTLS 连接到 SMTP 服务器,并且您最近在运行 PHPMailer 脚本的环境中没有更改任何内容,并且突然发生了这个问题 - 那么您可能需要检查服务器证书链中某处的过期或无效证书。

            您可以使用openssl s_client 查看服务器的证书链。

            对于端口 465 上的 SSL/TLS:

            openssl s_client -connect server.domain.tld:465 | openssl x509 -text
            

            对于端口 587 上的 STARTTLS:

            openssl s_client -starttls smtp -crlf -connect server.domain.tld:587 | openssl x509 -text
            

            【讨论】:

              【解决方案12】:

              在我的 CPANEL 中,我有“注册邮件 ID”选项,我在其中添加了我的电子邮件地址,30 分钟后它可以正常使用简单的 php 邮件功能。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-07-03
                • 1970-01-01
                • 1970-01-01
                • 2023-03-05
                • 1970-01-01
                相关资源
                最近更新 更多