2016-05-30 169 views
0

當我嘗試使用PHP SMTP電子郵件服務器發送電子郵件時,發生以下錯誤。SMTP錯誤:無法驗證。消息無法發送。郵件錯誤:SMTP錯誤:無法驗證

SMTP Error: Could not authenticate. Message could not be sent. 

郵件錯誤:SMTP錯誤:無法驗證。


以下是我用過的代碼。

function supervisorMail(){ 

global $error; 
$mail = new PHPMailer(); 
$mail->IsSMTP(); 
$mail->SMTPDebug = 0; 
$mail->SMTPAuth = true; 
$mail->SMTPSecure = 'ssl'; 
$mail->Host = 'smtp.gmail.com'; 
$mail->Port = 465; 
$mail->Username = "***@gmail.com"; 
$mail->Password = "****"; 
$mail->SetFrom("***@gmail.com", "Employee Leave Management System"); 

$userID=$_SESSION['userID']; 

$select_query = mysql_query("SELECT * FROM employee WHERE emp_id = '$userID'"); 
$select_sql = mysql_fetch_array($select_query); 
$name=$select_sql['manager_name']; 
var_dump($name); 

$select_query1 = mysql_query("SELECT email FROM employee WHERE emp_id='$name'"); 
$select_sql1 = mysql_fetch_array($select_query1); 
$email=$select_sql1['email']; 
    var_dump($email); 

$mail->Subject = "subject "; 
$mail->Body = "something."; 
$mail_to = $email; 
$mail->AddAddress($mail_to); 

if(!$mail->Send()) 
{ 
    echo "Message could not be sent. <p>"; 
    echo "Mailer Error: " . $mail->ErrorInfo; 
    exit; 
} 

echo "Message has been sent"; 

}

我該如何解決了這個錯誤。

+0

檢查我的答案,並嘗試http://stackoverflow.com/questions/37518499/could-not-connect-to-smtp-host-message-could-not-be-sent/37523865#37523865 –

+0

進入** $ mail->用戶名**和密碼** ** mail->密碼** –

回答

4

錯誤消息非常明確「無法驗證」。 我會說你正確使用PHPMailer類,但只是沒有填寫正確的Gmail帳戶的詳細信息。

我想,在你的問題中的字段的用戶名和密碼的樣子

$mail->Username = "@gmail.com"; 
$mail->Password = ""; 

僅僅是因爲你,很明顯,不希望與人分享,我會建議他們提出這樣的問題

$mail->Username = "********@gmail.com"; 
$mail->Password = "********"; 

因此,如果您使用的是正確的用戶名和密碼,還有其他兩件事情來檢查

  1. 也許您的設置「訪問不太安全的應用程序」已關閉。 後您從網絡登錄你可以從這個頁面打開它 https://www.google.com/settings/u/0/security/lesssecureapps

  2. 如果是開,有可能你有2個步驟驗證您的Gmail帳戶啓用。如果是這種情況,您需要創建一個應用程序特定的密碼。

按照一步地指導如何做到這一點 http://email.about.com/od/gmailtips/fl/How-to-Get-a-Password-to-Access-Gmail-By-POP-or-IMAP.htm

+0

我添加了正確的用戶名和密碼,但仍然是問題 – Chathurika

+0

@Chathurika我會嘗試使用我自己的帳戶憑據並看看發生了什麼......會讓你知道很快 –

+0

謝謝你,讓我知道代碼有什麼問題 – Chathurika

1

正如在評論中提及了這個鏈接,一個步驟中,您需要輸入有效的的Gmail ID密碼

請參考下面的演示代碼。

<?php 
require 'PHPMailer/PHPMailerAutoload.php'; 
$mail = new PHPMailer; 
$mail->SMTPDebug = 2; // Enable verbose debug output 
$mail->isSMTP(); // Set mailer to use SMTP 
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers 
$mail->SMTPAuth = true; // Enable SMTP authentication 
$mail->Username = '*****[email protected]'; // SMTP username 
$mail->Password = 'ma****02'; // SMTP password 
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted 
$mail->Port = 465; // TCP port to connect to 
$mail->setFrom('ravi******@gmail.com', 'Mailer'); // Add set from id 
$mail->addAddress('[email protected]', 'Ravi Hirani');  // Add a recipient 
//$mail->addAddress('[email protected]');    // Name is optional 
//$mail->addReplyTo('[email protected]', 'Information'); 
//$mail->addCC('[email protected]'); 
//$mail->addBCC('[email protected]'); 
//$mail->addAttachment('/var/tmp/file.tar.gz');   // Add attachments 
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name 
//$mail->isHTML(true); // Set email format to HTML 
$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'; 
} 
相關問題