2011-05-29 198 views
9

我想用phpMailer通過電子郵件向用戶發送確認消息。我的代碼是這樣的:PHPMailer將Gmail用作SMTP服務器。不能連接到SMTP主機。郵件錯誤:SMTP錯誤:無法連接到SMTP主機

<?php 
include("class.phpmailer.php"); 
include("class.smtp.php"); 
$mail = new PHPMailer(); 
$mail->IsSMTP(); // set mailer to use SMTP 
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server 
$mail->Port = 465; // set the port to use 
$mail->SMTPAuth = true; // turn on SMTP authentication 
$mail->Username = "[email protected]"; // your SMTP username or your gmail username 
$mail->Password = "mypasswrord"; // your SMTP password or your gmail password 
$from = "[email protected]"; // Reply to this email 
$to="[email protected]"; // Recipients email ID 
$name="Jersey Name"; // Recipient's name 
$mail->From = $from; 
$mail->FromName = "Webmaster"; // Name to indicate where the email came from when the recepient received 
$mail->AddAddress($to,$name); 
$mail->AddReplyTo($from,"Webmaster"); 
$mail->WordWrap = 50; // set word wrap 
$mail->IsHTML(true); // send as HTML 
$mail->Subject = "Sending Email From Php Using Gmail"; 
$mail->Body = "This Email Send through phpmailer, This is the HTML BODY "; //HTML Body 
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body 
if(!$mail->Send()) 
{ 
echo "Mailer Error: " . $mail->ErrorInfo; 
} 
else 
{ 
echo "Message has been sent"; 
} 
?> 

我已經在php.ini中啓用了SSL。

PS> [email protected]是保護隱私的掩碼郵件。但我沒有把一個真實的電子郵件地址,在該部分

+0

你有沒有考慮使用http://swiftmailer.org/?它可以通過GMail發送電子郵件。大圖書館! :-) – Flukey 2011-05-29 16:33:10

+0

此外,將主機從「ssl://smtp.gmail.com」更改爲「smtp.gmail.com」 – Flukey 2011-05-29 16:37:14

+0

什麼都沒有發生..甚至沒有回聲 – phpDeveloperForThesis 2011-05-29 16:50:36

回答

14

在你的php.ini確保您有未註釋與

extension=php_openssl.dll 
+2

這僅適用於windows ... Linux的適用範圍 – 2013-07-02 05:26:01

+2

對於Linux:extension = php_openssl.so – 2013-09-08 11:31:32

0

here

2)註釋掉下面的代碼行class.phpmailer.php

/* 
if(strstr($hosts[$index], ":")) 
list($host, $port) = explode(":", 
$hosts[$index]); else 
*/ 

試試這個,如果你還沒有已經。

+1

該行不存在於我的class.phpmailer.php – phpDeveloperForThesis 2011-05-29 16:51:34

0

This page有代碼作者聲稱「工作完美」。

唯一真正的區別我他之間看到和你的是,他有:

$mail->Mailer = "smtp"; 

我想我會開始與他的代碼完全看它是否工作,然後從那裏調試。

+0

仍然無法正常工作 – phpDeveloperForThesis 2011-05-29 16:47:54

+0

所以他的代碼適用於他,但不適合你?然後在環境中有不同的東西......嘗試'telnet smtp.gmail.com 465'以確保它不是防火牆問題。下一個最可能的原因是未能驗證服務器的SSL證書。 – Nemo 2011-05-29 16:54:29

2

行只啓用了extension=php_openssl.dll

,並按照this link

給出這些說明我已經測試過,它是工作100%。

0

錯誤:

$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server 

好:

$mail->Host = "smtp.gmail.com"; // specify main and backup server 
+1

這對我來說並不適用。它只適用於包含的「ssl://」。 – neave 2012-06-09 15:22:43