问题
I'm trying to send emails to user's accounts using PHPMailer, and I've been getting these errors:
The following From address failed: no-reply@random: MAIL FROM command failed,Authentication Required.
SMTP ERROR: MAIL FROM command failed Detail: Authentication Required.
SMTP server error: MAIL FROM command failed Detail: Authentication Required.
I've looked on overflow, the google support page supplied in the error, as well as the troubleshooting guide on github and haven't found any solutions. I know my credentials are right(I didn't post my real credentials on here). I don't want to have to allow less secure apps to access my email, because all users will have to for their emails as well. Also, I have tried oAuth2, but my redirect url(get_oauth_token.php is the file I used) couldn't find composer's autoload file : vendor/autoload.php, even though I have composer and guzzle downloaded and running.
Anyway here's the full debug output(SMTP debug is set to 2)
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP g198sm11047892itb.29 - gsmtp
CLIENT -> SERVER: EHLO localhost
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2602:306:ccb0:63b0:1817`enter code here`:970b:44c3:889b]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
CLIENT -> SERVER: EHLO localhost
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2602:306:ccb0:63b0:1817:970b:44c3:889b]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: MAIL FROM:<no-reply@random>
SERVER -> CLIENT: 530-5.5.1 Authentication Required. Learn more at530 5.5.1 https://support.google.com/mail/?p=WantAuthError g198sm11047892itb.29 - gsmtp
SMTP ERROR: MAIL FROM command failed: 530-5.5.1 Authentication Required. Learn more at530 5.5.1 https://support.google.com/mail/?p=WantAuthError g198sm11047892itb.29 - gsmtp
The following From address failed: no-reply@random: MAIL FROM command failed,Authentication Required. Learn more at https://support.google.com/mail/?p=WantAuthError g198sm11047892itb.29 - gsmtp,530,5.5.1SMTP server error: MAIL FROM command failed Detail: Authentication Required. Learn more at https://support.google.com/mail/?p=WantAuthError g198sm11047892itb.29 - gsmtp SMTP code: 530 Additional SMTP info: 5.5.1
Mailer Error: The following From address failed: no-reply@random: MAIL FROM command failed,Authentication Required. Learn more at https://support.google.com/mail/?p=WantAuthError g198sm11047892itb.29 - gsmtp ,530,5.5.1SMTP server error: MAIL FROM command failed Detail: Authentication Required. Learn more at https://support.google.com/mail/?p=WantAuthError g198sm11047892itb.29 - gsmtp SMTP code: 530 Additional SMTP info: 5.5.1SMTP server error: MAIL FROM command failed Detail: Authentication Required. Learn more at https://support.google.com/mail/?p=WantAuthError g198sm11047892itb.29 - gsmtp SMTP code: 530 Additional SMTP info: 5.5.1CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 closing connection g198sm11047892itb.29 - gsmtp
And here's the file I'm mailing from
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
require_once('PHPMailer/PHPMailer-master/PHPMailerAutoLoad.php');
require_once('PHPMailer/PHPMailer-master/class.smtp.php');
require_once('PHPMailer/PHPMailer-master/class.phpmailer.php');
date_default_timezone_set('Etc/UTC');
class Mail
{
public static function sendMail($subject,$body,$address)
{
//Create a new PHPMailer instance
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 1;
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = false;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Username = "random@gmail.com";
$mail->Password = "default";
$mail->SetFrom('no-reply@random');
$mail->addAddress($address);
$mail->Subject = $subject;
$mail->Body = $body;
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
}
?>
回答1:
Uh, it's failing to authenticate because you're telling it not to use authentication! Set $mail->SMTPAuth = true;
. If you want to see useful debug output, set SMTPDebug = 2
.
Also, there is absolutely no need to disable certificate checking when sending via gmail. It's a really bad idea - leave it enabled.
来源:https://stackoverflow.com/questions/44090384/need-assistance-with-authentication-errors-using-phpmailer