我在本地使用PHPMailer以及Apache和PHP。当我测试我的SSL配置和我的仙人掌时,我得到
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从本地主机发送邮件时,我总是
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)我还访问了SMTP中使用的帐户,并在https://accounts.google.com/DisplayUnlockCaptcha中启用它。以及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.发布于 2017-12-22 12:00:03
只是有同样的错误信息,也许你有同样的问题:
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中运行良好。
发布于 2019-01-14 10:23:04
最近,我在一个新的Windows IIS服务器上遇到了这种情况。cURL调用是从一个批处理脚本对我自己的域进行的,这两个脚本都运行在同一个服务器上。
我增加了
127.0.0.1 mydomain.com当我更改公共DNS时,忘记了删除它。
服务器位于Cloudflare的后面,所以我的cURL获得的是CF原产地证书,而不是Cloudflare提供的真正的证书。
删除主机条目解决了问题。
https://stackoverflow.com/questions/46511717
复制相似问题