问题
I get the following error when trying to use my GMail account to send an email from my PHP application with SwiftMailer.
535-5.7.8 Username and Password not accepted
This is my SwiftMailer code:
$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername('ayrshireminiscontact@gmail.com')
->setPassword('~password-in-here~');
$mailer = Swift_Mailer::newInstance($transporter);
$message = Swift_Message::newInstance('Portfolio Enquiry')
->setFrom(array('ayrshireminiscontact@gmail.com' => 'CRMPicco Portfolio Enquiry'))
->setTo(array('picco@crmpicco.co.uk' => 'A name'))
->setBody($email_body);
$result = $mailer->send($message);
This is the entry in my Apache error log and the stack trace.
[Wed Aug 18 22:06:10.284728 2015] [:error] [pid 9298] [client 10.0.0.1:64806] PHP Fatal error: Uncaught exception 'Swift_TransportException' with message 'Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at\r\n535 5.7.8 https://support.google.com/mail/answer/14257 gj13sm2191931wic.22 - gsmtp\r\n"' in /var/www/portfolio/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php:383\nStack trace:\n#0 /var/www/portfolio/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(281): Swift_Transport_AbstractSmtpTransport->_assertResponseCode('535-5.7.8 Usern...', Array)\n#1 /var/www/portfolio/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php(243): Swift_Transport_AbstractSmtpTransport->executeCommand('RSET\r\n', Array, Array)\n#2 /var/www/portfolio/swiftmailer/lib/classes/Swift/Transport/Esmtp/Auth/XOAuth2Authenticator.php(54): Swift_Transport_EsmtpTransport->executeCommand('RSET\r\n', Array)\n#3 /var/www/portfolio/swiftmailer/lib/classes/Swift/Transport/Esmtp/AuthHandler.php(176): Swift_Transport_Esmtp_Auth_XOAuth2Authent in /var/www/portfolio/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php on line 383, referer: https://crmpicco.dev/
I have tried http://www.google.com/accounts/DisplayUnlockCaptcha and I can log in to the account through a web browser without any problems.
This happens on my local development machine and the publically-accesible site. Is this an issue with my GMail account or my implementation?
回答1:
You need to allow access from less-secure apps, because IMAP via password is fundamentally insecure. (it's non-revocable)
Better yet, switch to OAuth authentication.
来源:https://stackoverflow.com/questions/32100825/gmail-username-and-password-not-accepted-error-when-sending-email-with-swiftmail