2010-08-13 212 views
30

我在几个项目上使用过PHPMailer,但现在我卡住了。它给我错误:
SMTP错误:无法连接到SMTP主机。
我试过从雷鸟发送电子邮件,它的工作原理!但不是通过PHPMailer的......这里是设置雷鸟:PHPMailer:SMTP错误:无法连接到SMTP主机

服务器名称:mail.exampleserver.com
端口:587
用户名:[email protected]
安全认证:没有
连接安全:STARTTLS

我在最后,我用我的PHPMailer的项目相比,这些与服务器和它们分别为:

服务器名称:mail.exampleserver2.com
端口:465
用户名:[email protected]
安全认证:没有
连接安全: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 

我在哪里错了?

+0

http://stackoverflow.com/questions/42471693/phpmailer-using-gmail-gives-error/42472146?noredirect=1#comment72085437_42472146 – 2017-02-26 18:45:21

回答

10

你的问题很可能是这个

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

这些是2个不同的协议,使用的是正确的,你使用任何雷鸟一个你需要使用。

尝试设定变量:

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

不存在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; 
42

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

所以我启用延长= php_openssl.dll

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

更新2017年

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

+1

启用这个PHP扩展( WAMP菜单选项)是我所需要的。谢谢。 – jwinn 2011-08-27 05:57:46

+0

你是我的英雄。我一直在我的本地主机上用电子邮件挣扎数小时,但这解决了我所有的问题! – 2012-04-04 16:47:20

+0

它为我节省了很多时间! – Bronek 2013-07-15 21:55:20

0

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

6

我有一个类似的问题,并且发现它是配置指令php.ini需要设置为允许验证安全对等方。您只需将其设置为证书颁发机构文件的位置,即可在http://curl.haxx.se/docs/caextract.html处获得的证书颁发机构文件。

该指令是PHP 5.6以来的新指令,因此从PHP 5.5升级时引起了我的警惕。

+0

我在同一个问题,使用PHP5.6,如果我把同行验证设置为false在phpmailer电子邮件转到垃圾邮件文件夹,任何想法如何设置证书路径?在哪里获得证书?在Debian 8 VPS – 2016-02-20 17:31:42

+0

Thx。对我来说,已经奏效。即:cd /etc/php5&&wget https://curl.haxx.se/ca/cacert.pem && echo“openssl.cafile = /etc/php5/cacert.pem”>>/etc/php5/apache2/php.ini – 2017-09-18 07:58:22

33

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

的PHPMailer的维基上的此一节:

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.

+3

你先生,救了我的命! – 2017-04-07 13:24:14

+2

对于那些与localhost邮件发生冲突并面临自签名问题的人来说,这是最好的答案。 – andromeda 2017-09-01 23:10:07

+0

出于某种原因,它工作 – 2017-11-27 09:39:57

2

我有同样的问题,这是因为PHPMailer的实现了服务器支持STARTTLS,所以它试图自动升级到加密连接的连接。我的邮件服务器与我的网络中的网络服务器位于同一子网中,这些服务器都位于我们的域防火墙之后,所以我不用担心使用加密(并且生成的电子邮件不包含敏感数据)。

因此,我所做的是在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; 
0

由于这是一个流行的错误,请查看PHPMailer Wiki有关疑难解答。

而且这个工作对我来说

$mailer->Port = '587'; 
相关问题