56

我在几个项目中使用了 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

我哪里错了?

4

12 回答 12

123

由于这个问题在谷歌中出现的频率很高,我想在这里分享我的解决方案,即 PHP 刚刚升级到 5.6 版(具有更严格的 SSL 行为)的情况。

PHPMailer wiki 对此有一个部分:

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 中所建议的那样,这应该是一个临时解决方案!

解决此问题的正确方法是用一个好的证书替换无效、配置错误或自签名的证书。

于 2016-04-04T14:32:56.127 回答
49

就我而言,是 PHP 中缺乏 SSL 支持导致了这个错误。

所以我启用了 extension=php_openssl.dll

$mail->SMTPDebug = 1;也暗示了这个解决方案。

2017 年更新

$mail->SMTPDebug = 2;,请参阅:https ://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#enabling-debug-output

于 2011-04-18T22:57:45.087 回答
12

你的问题很可能是这个

连接安全:STARTTLS 连接安全:SSL/TLS

这是 2 种不同的协议,您是否使用正确的一种,无论您在 Thunderbird 中使用哪种协议都需要使用。

尝试设置变量:

// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';
于 2010-08-13T15:41:13.957 回答
7

I had the same problem and it was because PHPMailer realized the server supported STARTTLS so it tried to automatically upgrade the connection to an encrypted connection. My mail server is on the same subnet as the web server within my network which is all behind our domain firewalls so I'm not too worried about using encryption (plus the generated emails don't contain sensitive data anyway).

So what I went ahead and did was change the SMTPAutoTLS to false in the class.phpmailer.php file.

/**
 * 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;
于 2017-01-31T21:26:52.227 回答
6

我遇到了类似的问题,并发现需要设置openssl.cafile配置指令以允许验证安全对等方。php.ini您只需将其设置为证书颁发机构文件的位置,就像您可以在http://curl.haxx.se/docs/caextract.html获得的文件一样。

这个指令是 PHP 5.6 的新指令,所以当我从 PHP 5.5 升级时,这让我措手不及。

于 2015-06-16T15:55:26.440 回答
5

以下代码对我有用:

$mail = new PHPMailer(true);

$mail->isSMTP();// Set mailer to use SMTP
$mail->CharSet = "utf-8";// set charset to utf8
$mail->SMTPAuth = true;// Enable SMTP authentication
$mail->SMTPSecure = 'tls';// Enable TLS encryption, `ssl` also accepted

$mail->Host = 'smtp.gmail.com';// Specify main and backup SMTP servers
$mail->Port = 587;// TCP port to connect to
$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);
$mail->isHTML(true);// Set email format to HTML

$mail->Username = 'Sender Email';// SMTP username
$mail->Password = 'Sender Email Password';// SMTP password

$mail->setFrom('example@mail.com', 'John Smith');//Your application NAME and EMAIL
$mail->Subject = 'Test';//Message subject
$mail->MsgHTML('HTML code');// Message body
$mail->addAddress('User Email', 'User Name');// Target email


$mail->send();
于 2018-06-27T13:54:28.843 回答
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;   
于 2010-12-27T16:19:49.870 回答
4
$mail->SMTPDebug = 2; // to see exactly what's the issue

就我而言,这有帮助:

$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;
于 2019-01-28T06:09:53.160 回答
1

由于这是一个常见错误,请查看PHPMailer Wiki以了解故障排除。

这也对我有用

$mailer->Port = '587';
于 2017-04-20T17:37:59.840 回答
0

我有一个类似的问题。我安装了不准备管理 SSL 连接的 PHPMailer 1.72 版。升级到最新版本解决了这个问题。

于 2012-05-22T12:49:31.833 回答
0

我最近处理了这个问题,问题的原因原来是我连接的 SMTP 服务器上的根证书是最近过期的 Sectigo 根证书

如果您通过 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
于 2020-06-10T18:45:48.257 回答
-1

在我的 CPANEL 中,我有“注册邮件 ID”选项,我在其中添加了我的电子邮件地址,30 分钟后它可以与简单的 php 邮件功能一起正常工作。

于 2019-02-07T13:09:58.793 回答