PHPMailer:SMTP 错误:无法连接到 SMTP 主机

PHPMailer: SMTP Error: Could not connect to SMTP host

提问人:Ilian Andreev 提问时间:8/13/2010 更新时间:11/18/2022 访问量:410041

问:

我已经在几个项目中使用了 PHPMailer,但现在我卡住了。它给了我错误:SMTP错误:
无法连接到SMTP主机。
我试过从 Thunderbird 发送电子邮件,它有效!但不是通过 PHPMailer ...以下是 Thunderbird 的设置:

服务器名称:mail.exampleserver.com
端口:587
用户名:[email protected]
安全身份验证:无
连接安全性:STARTTLS

我将它们与我使用 PHPMailer 的上一个项目的服务器进行了比较,它们是:

服务器名称:mail.exampleserver2.com
端口:465
用户名:[email protected]
安全身份验证:无
连接安全性: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

我哪里错了?

PHP 电子邮件 phpmailer

评论

0赞 blissweb 10/2/2021
对于那些在 2021 年 10 月收到此消息的人,请参阅以下内容:stackoverflow.com/questions/69413223/......

答:

15赞 Viper_Sb 8/13/2010 #1

你的问题很可能是这个

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

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

尝试设置变量:

// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';
5赞 TeAmEr 12/28/2010 #2

mail.exampleserver.com 存在???吗,如果没有,请尝试以下代码(您必须拥有Gmail帐户)

$mail->SMTPSecure = "ssl";  
$mail->Host='smtp.gmail.com';  
$mail->Port='465';   
$mail->Username   = '[email protected]'; // 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;   
49赞 wessel 4/19/2011 #3

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

所以我启用了extension=php_openssl.dll

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

更新 2017

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

评论

2赞 jwinn 8/27/2011
启用这个PHP扩展(WAMP菜单选项)是我所需要的。谢谢。
0赞 Mikkel R. Lund 4/5/2012
你是我的英雄。我已经在我的本地主机上为电子邮件苦苦挣扎了几个小时,但这解决了我所有的问题!
2赞 Faisal Ashfaq 4/18/2014
您是如何启用此扩展的?@wessel
0赞 Fernando Torres 4/28/2016
如何在 GoDaddy Hosting 中启用extension=php_openssl.dll?谢谢
0赞 David 5/22/2012 #4

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

7赞 Jasper 6/16/2015 #5

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

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

评论

0赞 Jobin 2/21/2016
我遇到了同样的问题,如果我在phpmailer电子邮件中将对等验证设置为false,请使用PHP5.6进入垃圾邮件文件夹,知道如何设置证书路径吗?从哪里获得证书?在Debian 8 VPS中
1赞 Szymon Wygnański 9/18/2017
感谢。对我来说,这奏效了。即:cd /etc/php5 && wget curl.haxx.se/ca/cacert.pem && echo “openssl.cafile = /etc/php5/cacert.pem” >> /etc/php5/apache2/php.ini
132赞 Marten Koetsier 4/4/2016 #6

由于这个问题在谷歌中排名很高,我想在这里分享我对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 中建议的那样,这应该是一个临时解决方案!

正确的解决方法是将无效、配置错误或自签名的证书替换为良好的证书。

评论

4赞 Cepheus 9/2/2017
对于那些在本地主机邮件中苦苦挣扎并面临自签名问题的人来说,这是最好的答案。
0赞 OzzyTheGiant 2/22/2018
同意!对于处于开发环境中的用户来说,这是最佳解决方案。只需将该代码包装在 if 语句中即可检查环境类型。如果是开发环境,请使用该代码,否则让服务器担心认证问题。
0赞 Mario 4/29/2018
最好的解决方案是,在用各种 PHPMailer 代码参数更改破坏了我的大脑数小时后,它最终成为一个错误或与较新的 PHP 版本不兼容。我现在在我的本地开发环境中使用 localhost 发送。干得好貂!
0赞 Fid 1/13/2019
非常感谢!已经寻找了两天的解决方案。
1赞 Fid 1/13/2019
旁注:我在 Postfix 中设置了错误的 LetsEncrypt 证书。它必须是 和 ,fullchain.pemprivkey.pem
7赞 Sina 2/1/2017 #7

我遇到了同样的问题,这是因为 PHPMailer 意识到服务器支持 STARTTLS,因此它尝试自动将连接升级到加密连接。我的邮件服务器与我网络中的 Web 服务器位于同一子网中,这些服务器都位于我们的域防火墙后面,所以我不太担心使用加密(而且生成的电子邮件无论如何都不包含敏感数据)。

因此,我继续做的是将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;

评论

3赞 TedMilker 11/16/2018
谢谢。不过,您无需编辑基类即可更改此设置,只需执行 $mail->SMTPAutoTLS = false;
1赞 Francis Sunday 4/21/2017 #8

由于这是一个流行的错误,请查看 PHPMailer Wiki 关于故障排除。

这也对我有用

$mailer->Port = '587';
5赞 Dumitru Boaghi 6/27/2018 #9

以下代码对我有用:

$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('[email protected]', '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();

评论

1赞 ron_g 11/5/2020
我持怀疑态度,但这让它起作用了。我认为 Gmail 需要设置 SMTPOptions,因为没有它我的就无法工作。
4赞 Martin Zvarík 1/28/2019 #10
$mail->SMTPDebug = 2; // to see exactly what's the issue

就我而言,这有助于:

$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;
-1赞 Dinesh Gurjar 2/7/2019 #11

就我而言,在CPANEL中,我有“注册邮件ID”选项,我在其中添加了我的电子邮件地址,30分钟后,它可以使用简单的php邮件功能。

2赞 mti2935 6/11/2020 #12

我最近处理了这个问题,问题的原因原来是我连接到的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
2赞 Waaal 7/22/2022 #13

好吧,这真的很旧,但我仍然想分享我的解决方案。 如果您将 phpmail 与本地服务器(如 xampp)一起使用,请关闭您的防病毒软件。 这为我解决了:)