我在几个项目中使用过PHPMailer但现在我被卡住了。它给了我错误:
SMTP错误:无法连接到SMTP主机。
我试过从Thunderbird发送电子邮件,它确实有效!但不是通过PHPMailer ......以下是Thunderbird的设置:
服务器名称:mail.exampleserver.com
港口:587
用户名:user@exampleserver.com
安全认证:否
连接安全性:STARTTLS
我在上一个使用PHPMailer的项目中将这些与服务器进行了比较,他们是:
服务器名称:mail.exampleserver2.com
港口:465
用户名:user@exampleserver2.com
安全认证:否
连接安全性:SSL / TLS
我的PHP代码是:
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = SMTP_HOST; // SMTP servers
$mail->Port = SMTP_PORT; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = SMTP_USER; // SMTP username
$mail->Password = SMTP_PASSWORD; // SMTP password
$mail->From = MAIL_SYSTEM;
$mail->FromName = MAIL_SYSTEM_NAME;
$mail->AddAddress($aSecuredGetRequest['email']);
$mail->IsHTML(true); // send as HTML
我哪里错了?
答案 0 :(得分:79)
由于这些问题在谷歌中显得很高,我想在这里分享我的解决方案,因为PHP刚刚升级到5.6版本(具有更严格的SSL行为)。
PHPMailer维基有一个关于此的部分:
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#php-56-certificate-verification-failure
建议的解决方法包括以下代码:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
这适用于PHPMailer 5.2.10(及以上版本)。
注意:显然,同样在wiki中建议,这应该是一个临时解决方案!
对此的正确解决方法是将错误的,配置错误的或自签名的证书替换为好的证书。
答案 1 :(得分:48)
在我的情况下,PHP中缺少SSL支持而导致此错误。
所以我启用了 extension = php_openssl.dll
$mail->SMTPDebug = 1;
也暗示了这个解决方案。
2017年更新
$mail->SMTPDebug = 2;
,请参阅:https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#enabling-debug-output
答案 2 :(得分:12)
你的问题很可能是这个
连接安全性:STARTTLS 连接安全性:SSL / TLS
这是两种不同的协议,你使用的是正确的协议,无论你在Thunderbird中使用的是什么,都需要使用。
尝试设置变量:
// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';
答案 3 :(得分:6)
我遇到了类似的问题,并且发现openssl.cafile
中的php.ini
配置指令需要设置为允许验证安全对等体。您只需将其设置为证书颁发机构文件的位置,就像您可以在http://curl.haxx.se/docs/caextract.html获得的那样。
这个指令是从PHP 5.6开始的新版本,因此从PHP 5.5升级时这让我措手不及。
答案 4 :(得分:6)
我遇到了同样的问题,因为PHPMailer意识到服务器支持STARTTLS所以它试图自动升级连接到加密连接。我的邮件服务器与我网络中的网络服务器在同一子网上,这完全在我们的域防火墙之后,所以我不太担心使用加密(加上生成的电子邮件不包含敏感数据)。
所以我要做的就是在class.phpmailer.php文件中将SMTPAutoTLS更改为false。
/**
* Whether to enable TLS encryption automatically if a server supports it,
* even if `SMTPSecure` is not set to 'tls'.
* Be aware that in PHP >= 5.6 this requires that the server's certificates are valid.
* @var boolean
*/
public $SMTPAutoTLS = false;
答案 5 :(得分:4)
mail.exampleserver.com存在吗??? ,如果没有尝试下面的代码(你必须有gmail帐户)
$mail->SMTPSecure = "ssl";
$mail->Host='smtp.gmail.com';
$mail->Port='465';
$mail->Username = 'you@gmail.com'; // SMTP account username
$mail->Password = 'your gmail password';
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->CharSet = 'utf-8';
$mail->SMTPDebug = 0;
答案 6 :(得分:2)
以下代码对我有用:
20-06-2018 USD 2.211,63
22-06-2018 USD 1.761,63
03-07-2018 USD 3.042,21
25-06-2018 USD 1.311,57
21-06-2018 USD 22.842,88
答案 7 :(得分:1)
$mail->SMTPDebug = 2; // to see exactly what's the issue
在我看来,这有所帮助:
$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;
答案 8 :(得分:0)
我有类似的问题。我安装了PHPMailer版本1.72,它不准备管理SSL连接。升级到上一版本解决了这个问题。
答案 9 :(得分:0)
答案 10 :(得分:0)
对于我在CPANEL中的情况,我有“注册邮件ID”选项,其中添加了我的电子邮件地址,并且30分钟后,它可以通过简单的php邮件功能正常工作。
答案 11 :(得分:0)
我最近处理了这个问题,结果是我连接的SMTP服务器上的根证书是Sectigo root certificate that recently expired。
如果您正在通过SSL / TLS或STARTTLS连接到SMTP服务器,并且您最近在运行PHPMailer脚本的环境中未进行任何更改,并且突然发生了此问题-那么您可能需要检查服务器证书链中某处的过期或无效证书。
您可以使用openssl s_client
查看服务器的证书链。
对于端口465上的SSL / TLS:
openssl s_client -connect server.domain.tld:465 | openssl x509 -text
对于端口587上的STARTTLS:
openssl s_client -starttls smtp -crlf -connect server.domain.tld:587 | openssl x509 -text