【问题标题】:PHPMailer do not work properly sometimes on gmail port 465 or 587 on localhostPHPMailer 有时在本地主机上的 gmail 端口 465 或 587 上无法正常工作
【发布时间】:2017-12-05 13:08:09
【问题描述】:

我在 LOCALHOST 上使用最新版本的 PHPMailer 6.0.2 与 Gmail SMTP 端口 [SSL on 465TLS 587]。它运行良好但不稳定或正常运行,很奇怪,而 OpenSSL 扩展也在 PHP Config /PHP.ini 文件中处于活动状态。

它返回错误“SMTP 错误:无法连接到 SMTP 主机”,有时在 465 或 587 端口上。

现在它在 587 上。

这里是端口 587 上的确切错误;

2017-12-05 13:00:26 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ( 'ssl' => array ( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true, ),)
2017-12-05 13:00:26 Connection: opened
2017-12-05 13:00:26 SMTP INBOUND: "220 smtp.gmail.com ESMTP f3sm245851pgt.15 - gsmtp"
2017-12-05 13:00:26 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP f3sm245851pgt.15 - gsmtp
2017-12-05 13:00:26 CLIENT -> SERVER: EHLO localhost
2017-12-05 13:00:27 SMTP INBOUND: "250-smtp.gmail.com at your service, [110.36.136.72]"
2017-12-05 13:00:27 SMTP INBOUND: "250-SIZE 35882577"
2017-12-05 13:00:27 SMTP INBOUND: "250-8BITMIME"
2017-12-05 13:00:27 SMTP INBOUND: "250-STARTTLS"
2017-12-05 13:00:27 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
2017-12-05 13:00:27 SMTP INBOUND: "250-PIPELINING"
2017-12-05 13:00:27 SMTP INBOUND: "250-CHUNKING"
2017-12-05 13:00:27 SMTP INBOUND: "250 SMTPUTF8"
2017-12-05 13:00:27 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [110.36.136.72]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-12-05 13:00:27 CLIENT -> SERVER: STARTTLS
2017-12-05 13:00:27 SMTP INBOUND: ""
2017-12-05 13:00:27 SERVER -> CLIENT: 
2017-12-05 13:00:27 SMTP ERROR: STARTTLS command failed: 
SMTP Error: Could not connect to SMTP host.
2017-12-05 13:00:27 SMTP NOTICE: EOF caught while checking if connected
2017-12-05 13:00:27 Connection: closed
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting     
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

这里是相关代码:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\OAuth;
use League\OAuth2\Client\Provider\Google;

require '../../vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 4;

$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;

$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->SMTPAutoTLS = false;

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

$mail->Username = $from_address;
$mail->Password = $from_password;
$mail->SetLanguage("tr", "phpmailer/language");
$mail->CharSet = "utf-8";
$mail->Encoding = "base64";
$mail->SetFrom($from_address, $from_name);

foreach ($to_email_list as $to) {
    $mail->AddAddress($to);
}

$mail->AddReplyTo($from_address, $from_name);
$mail->Subject = $email_subject;

//Creating Email Body
$message = "<html>\n";
$message .= "<body>\n";
$message .= '<p>Greetings,</p>';
$message .= '<p>' . $email_message . '</p>';
$message .= "</body>\n";
$message .= "</html>\n";
$mail->isHTML(true);
$mail->MsgHTML($message);

if(!$mail->Send()) {
    echo "On Port: " . $from_smtp_port . " </br> Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!  on Port " . $from_smtp_port . "</br>";

   foreach($to_email_list as $list){
        echo $list . "</br>";
    }

}

【问题讨论】:

  • 这是 TLS 失败。阅读并执行故障排除指南中的错误链接,了解如何诊断和解决问题。
  • 谢谢!能多指点一下吗?
  • 按照错误消息中的链接,按照它说的去做。
  • 因为,仍然无法在故障排除指南中找到解决方案。请多多指导。
  • 添加到您的问题中,当您使用 openssl 检查证书状态时会发生什么。在 PHP 中显示您的 openssl 扩展的状态。显示确保您的 CA 文件没有过期的结果。我不打算简单地重新输入指南中的内容。如果你不按那里所说的去做,我也没有理由相信你会在我在这里说同样的话时这样做。

标签: phpmailer


【解决方案1】:

终于在troubleshooting guide.找到了解决办法

我向其他面临相同问题的用户推荐;请仔细查看故障排除指南中的以下几点; - 机会性 TLS - PHP 5.6 证书验证失败 - cURL 错误 60

我的脚本在 Gmail ss/465 和 tls/587 上运行良好。

谢谢! @同步。

【讨论】:

    猜你喜欢
    • 2015-10-16
    • 2015-02-17
    • 1970-01-01
    • 2016-09-15
    • 2012-01-29
    • 2015-10-25
    • 2014-01-20
    • 1970-01-01
    • 2013-03-25
    相关资源
    最近更新 更多