2016-05-30 104 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'; 
} 
相关问题