【问题标题】:authentication failure [SMTP: STARTTLS failed (code: 220, response: 2.0.0 Ready to start TLS)]身份验证失败 [SMTP:STARTTLS 失败(代码:220,响应:2.0.0 准备启动 TLS)]
【发布时间】:2018-04-09 00:52:18
【问题描述】:

我正在尝试使用 SMTP 和 PEAR 在 PHP 中发送带有附件的电子邮件,但收到错误为“身份验证失败 [SMTP:STARTTLS 失败(代码:220,响应:2.0.0 准备启动 TLS)]”

<?php
require_once "Mail.php"; // PEAR Mail package
require_once ('Mail/mime.php'); // PEAR Mail_Mime packge

$from = "Your Mom <sender@gmail.com>";
$to = "Me <recepient address@gmail.com>";
$subject = 'Call Me!';

$headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);

// text and html versions of email.
$text = 'Hi son, what are you doing?nnHeres an picture of a cat for you.';
$html = 'Hi son, what are you doing?<br /><br />Here is an picture of a cat 
for you.';

// attachment
$file = 'fromc.xls';
$crlf = "n";

$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');

$body = $mime->get();
$headers = $mime->headers($headers);

$host = "smtp.gmail.com";
$username = "xyz@gmail.com";
$password = "xyz";

$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true,
 'username' => $username,'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
}
else {
echo("<p>Message successfully sent!</p>");
}
?>`

PHP版本:1.10.1 PEAR 版本:7.1.6
here 获取代码 请帮我清除错误...

【问题讨论】:

    标签: php email smtp pear


    【解决方案1】:

    如果你是自我认证的,我有一个更好的答案。

    添加:

    'auth' => true,
    'socket_options' => array('ssl' => array('verify_peer_name' => false, 'allow_self_signed' => true)),
    

    到 $smtp = Mail::factory('smtp', 行。

    本质上,您是在将它添加到数组中:

    allow_self_signed' => true
    

    这清楚地告诉代码允许使用 selt 证书。

    就我而言:

    $smtp = Mail::factory('smtp',array ('host' => $host,'auth' => true,'socket_options' => array('ssl' => array('verify_peer_name' => false, 'allow_self_signed' => true)),'username' => $username,'password' => $password,'port' => '25'));
    

    这与 Vlax 所说的类似,但没有奏效。我正在查看此链接并将其反转:

    https://github.com/PHPMailer/PHPMailer/issues/766

    【讨论】:

      【解决方案2】:

      未记录的参数:socket_options,让我在收到此错误时进行身份验证:
      身份验证失败 [SMTP:STARTTLS 失败(代码:220,响应:TLS 继续)]。

      我只需要添加:
      'auth' => "平原",
      'socket_options' => 数组('ssl' => 数组('verify_peer_name' => false)),

      取自:https://pear.php.net/manual/en/package.mail.mail.factory.php


      我收到了这个错误,但即使禁用 STARTTLS(如上述几个 cmets 所建议的那样)也无济于事,因为它随后报告了身份验证错误。我至少找到了适合我的情况的解决方法。

      如果您使用的是 PHP 5.6,SSL 会有一些变化: http://php.net/manual/en/migration56.openssl.php

      主要是对连接进行了额外的验证。此验证未在 5.5 上完成,因此这些问题被忽略了。但在我的情况下,服务器正在发送带有“localhost”的 SMTP EHLO 命令,显然这会导致 PHP 的新验证失败。

      解决方案是在 /include/pear/Net/SMTP.php 处修补 osTicket 的邮件类 - 更改此行:

      $this->_socket_options =$socket_options;

      $this->_socket_options = array('ssl' => array('verify_peer_name' => false));

      这会关闭验证。对于我的设置,邮件服务器与 osTicket 服务器在同一个本地网络上,所以我并不太担心安全性。

      另一种解决方案是降级到没有此额外验证的 PHP 5.5。

      如果 osTicket 以某种方式为此提供了一个设置,那就太好了,这样就不必每次都修补代码了。

      取自:https://github.com/pear/Net_SMTP/issues/14

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-22
        • 2023-04-04
        • 2014-08-17
        • 2014-01-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多