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

标签 php email phpmailer

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

我哪里错了?

最佳答案

由于这个问题在 google 中出现的频率很高,我想在这里分享一下我的解决方案,即 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 中所建议的,这应该是一个临时解决方案!

The correct fix for this is to replace the invalid, misconfigured or self-signed certificate with a good one.

关于PHPMailer:SMTP 错误:无法连接到 SMTP 主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3477766/

相关文章:

php - 如何保护 PHPMailer 文件的安全

PHP 打印 boolean 值是空的,为什么?

php - 如何在php中查找匹配查询的结果

php - 如何在PHP中获取当前日期,并在当前日期后加上1个月?

php - 通过 Google/Gmail 而不是从我的服务器发送电子邮件的优缺点

html - 如何在 outlook 电子邮件中创建 FORM

php - SMTP GMAIL 连接

php - 自定义 wordpress 25 主题 - 基本 PHP

javascript - 使用meteor js用户注册成功后发送电子邮件

php - 在 CC 中发送两个电子邮件 ID