PHPMailer:SMTP 错误:无法连接到 SMTP 主机 [英] PHPMailer: SMTP Error: Could not connect to SMTP host

查看:37
本文介绍了PHPMailer:SMTP 错误:无法连接到 SMTP 主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've used PHPMailer on several projects but now I'm stuck. It gives me the error:
SMTP Error: Could not connect to SMTP host.
I've tried sending email from Thunderbird and it works ! But not through PHPMailer ... Here are the settings from Thunderbird:

服务器名称:mail.exampleserver.com
端口:587
用户名:user@exampleserver.com
安全认证:否
连接安全:STARTTLS

我将这些与我上一个使用 PHPMailer 的项目中的服务器进行了比较,结果如下:

I've compared these with the server at my last project where I used PHPMailer and they were:

服务器名称: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

我错在哪里?

推荐答案

由于这个问题在 google 中出现率很高,我想在这里分享我针对 PHP 刚刚升级到 5.6 版(具有更严格的SSL 行为).

Since this questions shows up high in google, I'd like to share here my solution for the case where PHP was just upgraded to version 5.6 (which has stricter SSL behavior).

PHPMailer wiki 有一个关于此的部分:

The PHPMailer wiki has a section on this:

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#php-56-certificate-verification-failure

建议的解决方法是包含以下代码:

The suggested workaround is including the following piece of code:

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

这应该适用于 PHPMailer 5.2.10(及更高版本).

This should work for PHPMailer 5.2.10 (and up).

注意: 很明显,正如那个 wiki 中所建议的,这应该是一个临时解决方案!

Note: Obviously, and also as suggested in that wiki, this should be a temporary solution!

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

这篇关于PHPMailer:SMTP 错误:无法连接到 SMTP 主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆