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

时间:2022-04-04 18:12:18

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:

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

Server name: mail.exampleserver.com
Port: 587
Username: user@exampleserver.com
Secure Authentication: No
Connection Security: STARTTLS

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

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

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

Server name: mail.exampleserver2.com
Port: 465
Username: user@exampleserver2.com
Secure Authentication: No
Connection Security: SSL/TLS

服务器名称:mail.exampleserver2.com端口:465用户名:user@exampleserver2.com安全认证:无连接安全性:SSL / TLS

My php code is:

我的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

Where I am wrong?

哪里错了?

9 个解决方案

#1


54  

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).

由于这个问题在谷歌中显得很高,我想在这里分享我的解决方案,其中PHP刚刚升级到5.6版(具有更严格的SSL行为)。

The PHPMailer wiki has a section on this:

PHPMailer维基有一个关于此的部分:

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

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
    )
);

This should work for PHPMailer 5.2.10 (and up).

这适用于PHPMailer 5.2.10(及以上版本)。

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

注意:显然,并且在wiki中也是如此,这应该是一个临时解决方案!

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

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

#2


46  

In my case it was a lack of SSL support in PHP which gave this error.

在我的情况下,PHP中缺少SSL支持,这给出了这个错误。

So I enabled extension=php_openssl.dll

所以我启用了extension = php_openssl.dll

$mail->SMTPDebug = 1; also hinted towards this solution.

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

Update 2017

2017年更新

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

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

#3


10  

Your problem is most likely this

你的问题很可能就是这个问题

Connection Security: STARTTLS Connection Security: SSL/TLS

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

Those are 2 different protocols, are you using the correct one, whatever one you're using in Thunderbird needs to be used.

这些是两种不同的协议,你使用正确的协议,无论你在Thunderbird中使用什么,都需要使用。

Try setting the variable:

尝试设置变量:

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

#4


6  

I had a similar issue and figured out that it was the openssl.cafile configuration directive in php.ini that needed to be set to allow verification of secure peers. You just set it to the location of a certificate authority file like the one you can get at http://curl.haxx.se/docs/caextract.html.

我有一个类似的问题,并发现它是php.ini中的openssl.cafile配置指令,需要设置为允许验证安全对等体。您只需将其设置为证书颁发机构文件的位置,就像您可以在http://curl.haxx.se/docs/caextract.html获取的那样。

This directive is new as of PHP 5.6 so this caught me off guard when upgrading from PHP 5.5.

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

#5


4  

does mail.exampleserver.com exist ??? , if not try the following code (you must have gmail account)

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

$mail->SMTPSecure = "ssl";  
$mail->Host='smtp.gmail.com';  
$mail->Port='465';   
$mail->Username   = 'you@gmail.com'; // 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;   

#6


4  

I had the same problem and it was because PHPMailer realized the server supported STARTTLS so it tried to automatically upgrade the connection to an encrypted connection. My mail server is on the same subnet as the web server within my network which is all behind our domain firewalls so I'm not too worried about using encryption (plus the generated emails don't contain sensitive data anyway).

我有同样的问题,因为PHPMailer意识到服务器支持STARTTLS所以它试图自动升级连接到加密连接。我的邮件服务器与我网络中的Web服务器在同一子网上,这完全在我们的域防火墙后面,所以我不太担心使用加密(加上生成的电子邮件不包含敏感数据)。

So what I went ahead and did was change the SMTPAutoTLS to false in the class.phpmailer.php file.

所以我要做的就是在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;

#7


0  

I had a similar issue. I had installed PHPMailer version 1.72 which is not prepared to manage SSL connections. Upgrading to last version solved the problem.

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

#8


0  

Since this is a popular error, check out the PHPMailer Wiki on troubleshooting.

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

Also this worked for me

这也适合我

$mailer->Port = '587';

#9


0  

Followed code worked for me:

以下代码为我工作:

$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();

#1


54  

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).

由于这个问题在谷歌中显得很高,我想在这里分享我的解决方案,其中PHP刚刚升级到5.6版(具有更严格的SSL行为)。

The PHPMailer wiki has a section on this:

PHPMailer维基有一个关于此的部分:

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

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
    )
);

This should work for PHPMailer 5.2.10 (and up).

这适用于PHPMailer 5.2.10(及以上版本)。

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

注意:显然,并且在wiki中也是如此,这应该是一个临时解决方案!

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

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

#2


46  

In my case it was a lack of SSL support in PHP which gave this error.

在我的情况下,PHP中缺少SSL支持,这给出了这个错误。

So I enabled extension=php_openssl.dll

所以我启用了extension = php_openssl.dll

$mail->SMTPDebug = 1; also hinted towards this solution.

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

Update 2017

2017年更新

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

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

#3


10  

Your problem is most likely this

你的问题很可能就是这个问题

Connection Security: STARTTLS Connection Security: SSL/TLS

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

Those are 2 different protocols, are you using the correct one, whatever one you're using in Thunderbird needs to be used.

这些是两种不同的协议,你使用正确的协议,无论你在Thunderbird中使用什么,都需要使用。

Try setting the variable:

尝试设置变量:

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

#4


6  

I had a similar issue and figured out that it was the openssl.cafile configuration directive in php.ini that needed to be set to allow verification of secure peers. You just set it to the location of a certificate authority file like the one you can get at http://curl.haxx.se/docs/caextract.html.

我有一个类似的问题,并发现它是php.ini中的openssl.cafile配置指令,需要设置为允许验证安全对等体。您只需将其设置为证书颁发机构文件的位置,就像您可以在http://curl.haxx.se/docs/caextract.html获取的那样。

This directive is new as of PHP 5.6 so this caught me off guard when upgrading from PHP 5.5.

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

#5


4  

does mail.exampleserver.com exist ??? , if not try the following code (you must have gmail account)

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

$mail->SMTPSecure = "ssl";  
$mail->Host='smtp.gmail.com';  
$mail->Port='465';   
$mail->Username   = 'you@gmail.com'; // 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;   

#6


4  

I had the same problem and it was because PHPMailer realized the server supported STARTTLS so it tried to automatically upgrade the connection to an encrypted connection. My mail server is on the same subnet as the web server within my network which is all behind our domain firewalls so I'm not too worried about using encryption (plus the generated emails don't contain sensitive data anyway).

我有同样的问题,因为PHPMailer意识到服务器支持STARTTLS所以它试图自动升级连接到加密连接。我的邮件服务器与我网络中的Web服务器在同一子网上,这完全在我们的域防火墙后面,所以我不太担心使用加密(加上生成的电子邮件不包含敏感数据)。

So what I went ahead and did was change the SMTPAutoTLS to false in the class.phpmailer.php file.

所以我要做的就是在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;

#7


0  

I had a similar issue. I had installed PHPMailer version 1.72 which is not prepared to manage SSL connections. Upgrading to last version solved the problem.

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

#8


0  

Since this is a popular error, check out the PHPMailer Wiki on troubleshooting.

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

Also this worked for me

这也适合我

$mailer->Port = '587';

#9


0  

Followed code worked for me:

以下代码为我工作:

$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();