2012-10-09 128 views
1

我想發送郵件PHP。我爲本地主機和服務器使用相同的代碼。但是,當我使用服務器上的代碼,它似乎並沒有工作:在PHP發送郵件:SMTP錯誤:無法驗證

SMTP Error: Could not authenticate. Message was not sent.Mailer error: SMTP Error: Could not authenticate. 

這是我的代碼供您參考。

require("class.phpmailer.php"); // path to the PHPMailer class 

$mail = new PHPMailer(); 

$mail->IsSMTP(); // telling the class to use SMTP 
$mail->Mailer = "smtp"; 
$mail->Host = "ssl://smtp.gmail.com"; 
$mail->Port = 465; 
$mail->SMTPAuth = true; // turn on SMTP authentication 
$mail->Username = "myname"; // SMTP username 
$mail->Password = "password"; // SMTP password 

$mail->From = "[email protected]"; 
$mail->AddAddress("[email protected]"); 

$mail->Subject = "First PHPMailer Message"; 
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer."; 
$mail->WordWrap = 50; 

if(!$mail->Send()) { 
echo 'Message was not sent.'; 
echo 'Mailer error: ' . $mail->ErrorInfo; 
} else { 
echo 'Message has been sent.'; 
} 

我做了很多搜索,沒有彈出。任何幫助將不勝感激。

+0

你確定你已經給出正確的gmail ID和密碼嗎? –

+0

是的!它從本地主機發送郵件,但不從服務器發送。 –

+0

嘗試添加這個'$ mail-> SMTPSecure =「ssl」;' –

回答

0

它發生,因爲密碼包含特殊字符。我嘗試使用「\」之前像Password="djgh\^dfgfjk".特殊字符但它並沒有幫助我。
我只是創建新的ID和簡單的密碼沒有任何特殊的字符,令人驚訝的是它的工作。
使用無特殊字符的密碼。

0

只看看這可能對您有幫助:
它是用於smtp連接工作或不工作的telnet?在Windows 7防火牆阻止(默認)的SMTP連接,這樣, http://support.microsoft.com/kb/153119

1

嘗試TLS和端口587:

$mail->SMTPSecure = "tls"; 
$mail->Host  = "smtp.gmail.com"; 
$mail->Port  = 587; 

正如你可以看到你也可以指定要在SMTPSecure可變SSL連接和公正使用主機作爲smtp.gmail.com

也可以嘗試這個$mail->Mailer = "smtp";

0

一些託管服務提供商,如塊的1and1傳出的電子郵件端口,蘇更換此$mail->IsSMTP(); ch爲25,465和587. AFAIK的唯一解決方案是更改託管服務提供商。

試着做一個簡單的PHP文件,只是做了「fsokopen」你要使用的端口,看看是否能工程

0

它可能由於各種原因而發生。在我的情況下,它的工作,通過改變從

$phpmailer->isSMTP(); 

$phpmailer->Mailer  = 'smtp'; 

工作。

相關問題