2014-10-11 48 views
0

我目前處於我的項目的測試模式,並已設置PHPMAILER(版本5.2.9)庫,並希望使用Gmail SMTP生成電子郵件。PHP MAILER GMAIL SMTP錯誤

我已經在我的本地系統上設置了腳本,但是當腳本執行時,它始終等待本地主機響應,即使我指定了smtp服務器地址。

我目前使用下列內容:

PHP版本5.4.7 XAMPP版本1.8.1(它是過時的,我知道) PHPMailer的版本 - 5.2.9

下面

是腳本:

require 'phpmailer/PHPMailerAutoload.php'; 
$mail = new PHPMailer; 

$mail->SMTPDebug = 3; 
$mail->isSMTP(); 
$mail->Host = 'smtp.gmail.com'; 
$mail->SMTPAuth = true; 
$mail->Username = '[email protected]'; 
$mail->Password = 'xxx'; 
$mail->SMTPSecure = 'tls'; 
$mail->Port = 587; 

$mail->From = '[email protected]'; 
$mail->FromName = 'test'; 
$mail->addAddress('[email protected]'); 
$mail->WordWrap = 50; 
$mail->isHTML(true); 
$mail->Subject = 'Here is the subject'; 
$mail->Body = 'This is the HTML message body <b>in bold!</b>'; 
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 

if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 
    echo 'Message has been sent'; 
} 

感謝

+0

您的代碼看起來不錯,但你必須張貼實際的錯誤。 – Synchro 2014-10-11 08:06:07

+0

您好同步,borwser不斷等待本地主機的響應。我等了幾分鐘,但沒有迴應。如果我將「tls」更改爲「starttls」,那麼它會給出錯誤,但由於charachter長度限制,我無法將其發佈到此處。但最後它說: 郵件無法發送。郵件錯誤:SMTP連接()失敗。 – dk007 2014-10-11 14:57:03

+0

不要在評論中發佈那樣的大事 - 他們不太可讀。改爲編輯你的問題。在這種情況下,它看起來像你的PHP是沒有SSL編譯的,所以在你添加SSL之前你可以做的事情不多。該設置應該是'tls'或'ssl'; 'starttls'不是一個有效的選項。 – Synchro 2014-10-11 16:30:49

回答

0

你的代碼應該是:

require 'phpmailer/PHPMailerAutoload.php'; 

    $mail = new PHPMailer; 
    //Tell PHPMailer to use SMTP 
    $mail->isSMTP(); 
    //Enable SMTP debugging 
    // 0 = off (for production use) 
    // 1 = client messages 
    // 2 = client and server messages 
    $mail->SMTPDebug = 2; 
    //Ask for HTML-friendly debug output 
    $mail->Debugoutput = 'html'; 
    //Set the hostname of the mail server 
    $mail->Host = 'smtp.gmail.com'; 
    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission 
    $mail->Port = 587; 
    $mail->SMTPSecure = 'tls'; 
    $mail->SMTPAuth = true; 
    $mail->Username = "[email protected]"; 
    $mail->Password = "yourpassword"; 

    //Set who the message is to be sent from 
    $mail->setFrom('[email protected]', 'First Last'); 
    $mail->addReplyTo('[email protected]', 'First Last'); 
    $mail->addAddress('[email protected]', 'John Doe'); 
    $mail->WordWrap = 50;         
    $mail->isHTML(true);         
    $mail->Subject = 'Here is the subject'; 
    $mail->Body = 'This is the HTML message body <b>in bold!</b>'; 
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 

    if(!$mail->send()) { 
     echo 'Message could not be sent.'; 
     echo 'Mailer Error: ' . $mail->ErrorInfo; 
    } else { 
     echo 'Message has been sent'; 
    } 

沒有爲$mail->SMTPDebug = 3;

+0

是的。另外4. – Synchro 2014-10-11 08:04:46

+0

嗨hardik,borwser不斷等待本地主機的響應。我等了幾分鐘,但沒有迴應。如果我將「tls」更改爲「starttls」,那麼它會給出錯誤,但由於charachter長度限制,我無法將其發佈到此處。但最後說:無法發送消息。郵件錯誤:SMTP連接()失敗。 – dk007 2014-10-11 14:57:20

0

別無選擇,如此看來,這個問題是在php.ini文件中的設置。 un-comment; extension = php_openssl.dll通過刪除前面的;它的工作。

0

確保您的phpmailer腳本運行正常,如果郵件程序沒有問題,請使用您的Gmail帳戶登錄並轉到 - > gmail設置。 現在檢查gmail設置中允許不太安全的選項。 默認情況下,它的關閉

允許不夠安全的應用:OFF

確保此設置爲開啓狀態。

一旦完成,現在從瀏覽器運行腳本。

如果你沒有找到鏈接點擊此處進入 https://myaccount.google.com/security#activity

0

Try if SMTP issues + using TSL+ Gmail 
 
<?php 
 
include'PHPMailer/PHPMailerAutoload.php'; 
 

 
include "PHPMailer/class.phpmailer.php"; // include the class name 
 
$mail = new PHPMailer(); // create a new object 
 
$mail->IsSMTP(); // enable SMTP 
 

 
$mail->SMTPOptions = array(
 
    'ssl' => array(
 
     'verify_peer' => false, 
 
     'verify_peer_name' => false, 
 
     'allow_self_signed' => true 
 
    ) 
 
); 
 

 
//$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only 
 
//$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only 
 
$mail->SMTPAuth = true; // authentication enabled 
 
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail 
 
$mail->Host = "smtp.gmail.com"; 
 
$mail->Port = 587; // or 587 
 
$mail->IsHTML(true); 
 
$mail->Username = "[email protected]"; 
 
$mail->Password = "mypassword"; 
 
$mail->addReplyTo("[email protected]","user"); 
 
$mail->SetFrom("[email protected]","My Site"); 
 
$mail->Subject = "Your Gmail SMTP Mail"; 
 
$mail->Body = "Hi, your first SMTP mail via gmail server has been received."; 
 
$mail->AddAddress("[email protected]"); 
 
if(!$mail->Send()){ 
 
    echo "Mailer Error: " . $mail->ErrorInfo; 
 
} 
 
else{ 
 
    echo "Message has been sent"; 
 
} 
 
?>

相關問題