当我尝试使用PHP 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 :(得分:4)
错误消息非常明确“无法进行身份验证”。 我会说你正确使用PHPMailer类,但只是没有填写正确的gmail帐户详细信息。
我想在你的问题中,字段用户名和密码看起来像
$mail->Username = "@gmail.com";
$mail->Password = "";
只是因为你显然不想分享它们,我建议在问题中这样提出它们
$mail->Username = "********@gmail.com";
$mail->Password = "********";
因此,如果您使用正确的用户名和密码,还有其他两件事需要检查
也许您的设置“访问安全性较低的应用”已关闭。 从Web登录后,您可以从此页面打开它 https://www.google.com/settings/u/0/security/lesssecureapps
如果启用,则可能在您的Gmail帐户中启用了两步验证。如果是这种情况,则需要创建应用专用密码。
请按照此链接获取有关如何操作的分步指南 http://email.about.com/od/gmailtips/fl/How-to-Get-a-Password-to-Access-Gmail-By-POP-or-IMAP.htm
答案 1 :(得分: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 = '*****001@gmail.com'; // 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('er.ravihirani@gmail.com', 'Ravi Hirani'); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//$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';
}
答案 2 :(得分:0)
我遇到了这条错误消息,想发布一个答案,以防它可以帮助别人。我有phpmailer安装与composer并运行php 7.执行phpmailer脚本抛出错误是在php对象方法。在其他地方的配置文件中设置的密码变量需要在对象上下文中声明为全局,否则它不包含实际密码,从而导致身份验证错误。
global $password; // <- this added to make script work
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
// Set mailer to use SMTP
$mail->Host = '*******.com';
$mail->SMTPAuth = true;
$mail->Username = 'no-reply@*******.com';
$mail->Password = $password;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('no-reply@*******.com', '******* - DO NOT REPLY');
//$mail->isHTML(true);
$mail->AddAddress($user->email);
$mail->Subject = "*******";
$mail->Body = "*******";
$mail->send();
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
答案 3 :(得分:0)
就我而言,这是因为我没有从以下位置打开缺乏安全的应用访问权限:https://myaccount.google.com/lesssecureapps
答案 4 :(得分:0)
请关闭您不太安全的应用 接着 尝试对此行发表评论
/// $ mail-> IsSMTP();
我希望它能起作用!