php PHPMailer:SMTP 错误:无法连接到 SMTP 主机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3477766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHPMailer: SMTP Error: Could not connect to SMTP host
提问by Ilian Andreev
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: [email protected]
Secure Authentication: No
Connection Security: STARTTLS
服务器名称:mail.exampleserver.com
端口:587
用户名:[email protected]
安全认证:无
连接安全: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: [email protected]
Secure Authentication: No
Connection Security: SSL/TLS
服务器名称:mail.exampleserver2.com
端口:465
用户名:[email protected]
安全认证:无
连接安全: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?
我错在哪里?
回答by Marten Koetsier
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).
由于这个问题在 google 中出现率很高,我想在这里分享我的解决方案,用于 PHP 刚刚升级到版本 5.6(具有更严格的 SSL 行为)的情况。
The PHPMailer wiki has a section on this:
PHPMailer wiki 有一个关于此的部分:
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.
对此的正确解决方法是用一个好的证书替换无效的、配置错误的或自签名的证书。
回答by wessel
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
回答by Viper_Sb
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.
这是 2 种不同的协议,您使用的是正确的一种吗,无论您在 Thunderbird 中使用的是什么,都需要使用。
Try setting the variable:
尝试设置变量:
// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';
回答by Sina Motamedi
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;
回答by Jasper
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.
我有一个类似的问题,并发现它是需要设置的openssl.cafile
配置指令php.ini
以允许验证安全对等点。您只需将其设置为证书颁发机构文件的位置,就像您可以在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 升级时,这让我措手不及。
回答by Rami Dabain
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 = '[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;
回答by Martin Zvarík
$mail->SMTPDebug = 2; // to see exactly what's the issue
In my case this helped:
就我而言,这有帮助:
$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;
回答by Dumitru Boaghi
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('[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();
回答by Francis Sunday
Since this is a popular error, check out the PHPMailer Wikion troubleshooting.
由于这是一个常见错误,请查看PHPMailer Wiki进行故障排除。
Also this worked for me
这也对我有用
$mailer->Port = '587';
回答by David
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 连接。升级到最新版本解决了这个问题。