我在本地使用PHPMailer,以及Apache和PHP。 当我测试我的SSL配置和我的cacerts时,我得到了
default_cert_file = C:\Program Files\Common Files\SSL/cert.pem
default_cert_file_env = SSL_CERT_FILE
default_cert_dir = C:\Program Files\Common Files\SSL/certs
default_cert_dir_env = SSL_CERT_DIR
default_private_dir = C:\Program Files\Common Files\SSL/private
default_default_cert_area = C:\Program Files\Common Files\SSL
ini_cafile =
ini_capath =
然后,我做
var_dump(fsockopen("smtp.gmail.com", 465, $errno, $errstr, 3.0));
var_dump($errno);
var_dump($errstr);
我得到了
fsockopen resource(2) of type (stream) int(0) string(0) ""
在我的php.ini
curl.cainfo = C:/php/cacert.pem
部分curl
中,它有效。
但是当我尝试使用PHPMailer从localhost发送邮件时,我一直在
SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
Failed to enable crypto
unable to connect to ssl://smtp.gmail.com:465 (Unknown error)
我还在https://accounts.google.com/DisplayUnlockCaptcha
中访问了SMTP中使用的帐户并启用它。并https://myaccount.google.com/lesssecureapps?pli=1
并启用较少安全的应用。
我想这是一个SSL错误而不是PHPMailer。坦率地说,我很困惑,不知道如何解决它。请原谅我的无知,任何关于什么是错误的帮助以及如何解决它都会很棒。
PS,这是我发送邮件的php文件。谢谢
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'C:/php/PHPMailer/src/Exception.php';
require 'C:/php/PHPMailer/src/PHPMailer.php';
require 'C:/php/PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'slevin@gmail.com';
$mail->Password = 'secret';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
//Recipients
$mail->setFrom('slevin@gmail.com');
$mail->addAddress('jacob@gmail.com');
//Content
$mail->isHTML(true);
$mail->Subject = 'subject';
$mail->Body = 'HTML message body <b>in bold!</b>';
$mail->AltBody = 'body in plain text';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
更新
当我尝试使用$mail->SMTPSecure = 'ssl';
和$mail->Port = 465;
时,我得到了
2017-10-01 13:04:25 Connection: opening to ssl://smtp.gmail.com:465, timeout=300, options=array()
2017-10-01 13:04:26 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:\php\PHPMailer\src\SMTP.php line 324]
2017-10-01 13:04:26 Connection failed. Error #2: stream_socket_client(): Failed to enable crypto [C:\php\PHPMailer\src\SMTP.php line 324]
2017-10-01 13:04:26 Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) [C:\php\PHPMailer\src\SMTP.php line 324]
2017-10-01 13:04:26 SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
当我尝试使用$mail->SMTPSecure = 'tls';
和$mail->Port = 587;
时,我得到了
2017-10-01 13:07:20 Connection: opening to smtp.gmail.com:587, timeout=300, options=array()
2017-10-01 13:07:21 Connection: opened
2017-10-01 13:07:21 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP l4sm5217189wrb.74 - gsmtp
2017-10-01 13:07:21 CLIENT -> SERVER: EHLO localhost
2017-10-01 13:07:21 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [85.75.196.114]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250 SMTPUTF8
2017-10-01 13:07:21 CLIENT -> SERVER: STARTTLS
2017-10-01 13:07:21 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2017-10-01 13:07:21 Connection failed. Error #2: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed [C:\php\PHPMailer\src\SMTP.php line 403]
SMTP Error: Could not connect to SMTP host.
2017-10-01 13:07:21 CLIENT -> SERVER: QUIT
2017-10-01 13:07:21 SERVER -> CLIENT:
2017-10-01 13:07:21 SMTP ERROR: QUIT command failed:
2017-10-01 13:07:21 Connection: closed
SMTP Error: Could not connect to SMTP host.
Message could not be sent.Mailer Error: SMTP Error: Could not connect to SMTP host.
答案 0 :(得分:4)
刚刚收到相同的错误消息,也许你有同样的问题:
stream_context_create
和stream_socket_client
通过cURL测试同一个域:
curl: (60) SSL certificate problem: certificate is not yet valid
原来我运行PHP和cURL的虚拟机有11天的错误(12月11日而不是12月22日)。
通过修复计算机/虚拟机的日期(使用ntp
,ntpdate-debian
等),证书测试现在可以在cURL命令行或PHP。
答案 1 :(得分:0)
我最近在新的Windows IIS服务器上遇到此问题。 cURL调用是从批处理脚本到我自己的域的,这两个脚本都在同一服务器上运行。
我添加了
127.0.0.1 mydomain.com
到hosts文件,当我更改公共DNS时,我忘记将其删除。
服务器位于Cloudflare的后面,所以我的cURL得到的是CF来源证书,而不是Cloudflare提供的真实证书。
删除主机条目可以解决此问题。
答案 2 :(得分:0)
我总是遇到相同的错误,但是当我禁用我的防病毒软件时,此问题已解决。我认为禁用防病毒软件也可以解决您的问题。防病毒有时会阻止该应用程序。