提问人:Ilian Andreev 提问时间:8/13/2010 更新时间:11/18/2022 访问量:410041
PHPMailer:SMTP 错误:无法连接到 SMTP 主机
PHPMailer: SMTP Error: Could not connect to SMTP host
问:
我已经在几个项目中使用了 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
我哪里错了?
答:
你的问题很可能是这个
连接安全性:STARTTLS 连接安全性:SSL/TLS
这是 2 个不同的协议,你是否使用了正确的协议,无论你在 Thunderbird 中使用的哪个协议都需要使用。
尝试设置变量:
// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';
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;
就我而言,PHP中缺乏SSL支持导致了此错误。
所以我启用了extension=php_openssl.dll
$mail->SMTPDebug = 1;
也暗示了这个解决方案。
更新 2017
$mail->SMTPDebug = 2;
,请参阅:https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#enabling-debug-output
评论
我也有类似的问题。我已经安装了 PHPMailer 1.72 版,它不准备管理 SSL 连接。升级到最新版本解决了这个问题。
我遇到了类似的问题,并发现需要设置配置指令以允许验证安全对等体。您只需将其设置为证书颁发机构文件的位置,例如您可以在 http://curl.haxx.se/docs/caextract.html 获得的文件。openssl.cafile
php.ini
这个指令是 PHP 5.6 的新指令,所以从 PHP 5.5 升级时这让我措手不及。
评论
由于这个问题在谷歌中排名很高,我想在这里分享我对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 中建议的那样,这应该是一个临时解决方案!
正确的解决方法是将无效、配置错误或自签名的证书替换为良好的证书。
评论
fullchain.pem
privkey.pem
我遇到了同样的问题,这是因为 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;
评论
由于这是一个流行的错误,请查看 PHPMailer Wiki 关于故障排除。
这也对我有用
$mailer->Port = '587';
以下代码对我有用:
$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();
评论
$mail->SMTPDebug = 2; // to see exactly what's the issue
就我而言,这有助于:
$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;
就我而言,在CPANEL中,我有“注册邮件ID”选项,我在其中添加了我的电子邮件地址,30分钟后,它可以使用简单的php邮件功能。
我最近处理了这个问题,问题的原因原来是我连接到的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
好吧,这真的很旧,但我仍然想分享我的解决方案。 如果您将 phpmail 与本地服务器(如 xampp)一起使用,请关闭您的防病毒软件。 这为我解决了:)
评论