我使用的是PHP 7.4.6版本。我下载了PHPMailer的主版本,从 https:/github.comPHPMailerPHPMailer。.
我的PHPMailer技能的一个小背景:三天前我开始使用它来建立一个简单的联系网站的基本联系表格。
我收到以下错误。
2020-05-22 16:51:05 Connection: opening to ssl://smtp.gmail.com:465, timeout=300, options=array()
2020-05-22 16:51:05 Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed [C:\xamp\htdocs\PHPMailer-master\src\SMTP.php line 344]
2020-05-22 16:51:05 Connection failed. Error #2: stream_socket_client(): Failed to enable crypto [C:\xamp\htdocs\PHPMailer-master\src\SMTP.php line 344]
2020-05-22 16:51:05 Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) [C:\xamp\htdocs\PHPMailer-master\src\SMTP.php line 344]
2020-05-22 16:51:05 SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message has been sent
它显示我 '消息已发送'伴随着错误。
stream_socket_client(): SSL操作失败,代码为1。OpenSSL错误信息:错误:1416F086:SSL例程:tls_process_server_certificate:证书验证失败。
下面是我所做的改动。
下面是我的代码。
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require('PHPMailer-master/src/Exception.php');
require('PHPMailer-master/src/PHPMailer.php');
require('PHPMailer-master/src/SMTP.php');
// Load Composer's autoloader
require 'PHPMailer-master/src/PHPMailerAutoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer;
try {
//Server settings
$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'PASSWORD'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption;`PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 465; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('[email protected]');
$mail->addAddress('[email protected]');
$mail->addReplyTo('[email protected]');
// Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
谢谢你检查我的代码.
我正在努力阅读这个平台上给出的尽可能多的问题和答案。但是到目前为止,我没有看到任何一个问题和我现在面临的问题类似。
同时,我也会继续寻找解决方案。
干杯!
PS:如果有拼写错误,请不要介意。我知道有些人很在意这个问题。
更新(2352020)。 我把php文件发给了我朋友。当他运行时,他没有收到任何错误,而且他那边的信息也成功发送了。
你的代码之所以会出现 "消息已发送 "的情况,是因为虽然你用 trycatch 块封装了它,但你没有告诉 PHPMailer 抛出异常,而 PHPMailer 默认情况下是不会抛出异常的--通过传入 true
到构造函数中。
$mail = new PHPMailer(true);
如果你真的有最新版本的PHPMailer,那么这一行将导致一个致命的错误。
require 'PHPMailer-masterrcPHPMailerAutoload.php'。
因为这个文件是旧版本的,已经不存在于PHPMailer中了。
你确实是TLS的问题。一般来说,如果你使用了以下方法,调试TLS问题会更容易。SMTPSecure = 'tls'
和 Port = 587
因为你会得到更多的调试输出,特别是你会看到来自SMTP服务器的初始欢迎横幅,这可能揭示了你的SMTP流量正在被拦截并重定向到你的主机提供商的服务器上--而TLS很正确地拒绝了,理由是它的主机名与你所期望的不匹配。
我也建议你使用 openssl s_client
如同你的错误信息中的故障排除指南中所描述的那样,这将消除你的PHP配置作为问题的来源。