2010-08-13 84 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'; 
相關問題