Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHPMailer:SMTP错误:无法连接到SMTP主机_Php_Email_Phpmailer - Fatal编程技术网

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

PHPMailer:SMTP错误:无法连接到SMTP主机,php,email,phpmailer,Php,Email,Phpmailer,我在几个项目中使用了PHPMailer,但现在我被卡住了。它给了我一个错误: SMTP错误:无法连接到SMTP主机。 我试着从雷鸟那里发送电子邮件,效果很好!但不是通过PHPMailer。。。以下是Thunderbird的设置: 服务器名称:mail.exampleserver.com 端口:587 用户名:user@exampleserver.com 安全身份验证:否 连接安全性:STARTTLS 我将其与我上次使用PHPMailer的项目中的服务器进行了比较,结果是: 服务器名称:mail.

我在几个项目中使用了PHPMailer,但现在我被卡住了。它给了我一个错误:
SMTP错误:无法连接到SMTP主机。
我试着从雷鸟那里发送电子邮件,效果很好!但不是通过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

我哪里错了?

你的问题很可能是这个

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

这是两个不同的协议,您使用的是正确的协议吗?无论您在Thunderbird中使用的是什么协议,都需要使用

尝试设置变量:

// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';

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


在我的例子中,是PHP中缺少SSL支持导致了这个错误

所以我启用了extension=php\u openssl.dll

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

2017年更新


$mail->SMTPDebug=2,请参阅:

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

我遇到了一个类似的问题,并发现需要设置
php.ini
中的
openssl.cafile
配置指令以允许验证安全对等方。您只需将其设置为证书颁发机构文件的位置,就像您可以访问的位置一样


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

由于这个问题在google中很常见,我想在这里分享我的解决方案,以解决PHP刚刚升级到版本5.6(具有更严格的SSL行为)的情况

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

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

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);
这适用于PHPMailer 5.2.10(及更高版本)

注意:显然,也正如维基中所建议的,这应该是一个临时解决方案

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


我也遇到了同样的问题,因为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;

由于这是一个常见错误,请查看有关疑难解答的

这对我也有用

$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('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();
就我而言,这有助于:

$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;

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

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

如果您通过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

启用这个PHP扩展(WAMP菜单选项)是我所需要的。谢谢你,你是我的英雄。我已经在本地主机上挣扎了几个小时,但这解决了我所有的问题!您是如何启用此扩展的@Wessel如何在GoDaddy主机中启用extension=php_openssl.dll?谢谢,我也有同样的问题,使用PHP5.6如果我在phpmailer emails goes to spam文件夹中将peer-verification设置为false,你知道如何设置证书路径吗?在哪里可以拿到证书?在Debian 8 VPSThx中。对我来说,这已经奏效了。也就是说:cd/etc/php5&&wget&&echo“openssl.cafile=/etc/php5/cacert.pem”>>/etc/php5/apache2/php.init这是解决本地主机邮件和面临自签名问题的最佳答案。同意!这是开发环境中的最佳解决方案。只需将该代码包装在if语句中,以检查环境类型。如果是在开发环境中,请使用该代码,否则就让服务器担心认证问题。最佳解决方案是,在经过数小时的PHP编译器代码参数的各种更改后,它最终成为一个bug或与较新的PHP版本不兼容。我现在在本地开发环境中使用localhost发送。干得好,貂皮!非常感谢!两天来一直在寻找解决方案。旁注:我的LetsEncrypt证书在后缀中设置错误。它必须是
fullchain.pem
privkey.pem
,谢谢。您不需要编辑基类来更改它,只需执行$mail->SMTPAutoTLS=false;我很怀疑,但这让它起作用了。我认为Gmail需要SMTPOptions设置,因为没有它我就无法工作。
openssl s_client -connect server.domain.tld:465 | openssl x509 -text
openssl s_client -starttls smtp -crlf -connect server.domain.tld:587 | openssl x509 -text