使用SwiftMailer发送电子邮件时,GMail用户名和密码不被接受的错误
尝试使用我的GMail帐户从我的 PHP应用程序发送电子邮件时,出现以下错误>与SwiftMailer.
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
这是我的SwiftMailer代码:
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);
这是我的Apache错误日志和堆栈跟踪中的条目.
This is the entry in my Apache error log and the stack trace.
[2015年8月18日星期三22:06:10.284728] [:错误] [pid 9298] [客户端 10.0.0.1:64806] PHP致命错误:消息为预期响应代码250"的未捕获异常'Swift_TransportException' 但得到的代码为"535",消息为"535-5.7.8",用户名和密码不正确 公认.在\ 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 跟踪:\ n#0 /var/www/portfolio/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(281): Swift_Transport_AbstractSmtpTransport-> _ assertResponseCode('535-5.7.8 Usern ...',数组)\ n#1 /var/www/portfolio/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php(243): Swift_Transport_AbstractSmtpTransport-> executeCommand('RSET \ r \ n', 阵列,阵列)\ n#2 /var/www/portfolio/swiftmailer/lib/classes/Swift/Transport/Esmtp/Auth/XOAuth2Authenticator.php(54): Swift_Transport_EsmtpTransport-> executeCommand('RSET \ r \ n', 阵列)\ 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 在383行上,引荐来源网址: https://crmpicco.dev/
[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/
我尝试了 http://www.google.com/accounts/DisplayUnlockCaptcha 和我可以通过网络浏览器登录到该帐户,而不会出现任何问题.
I have tried http://www.google.com/accounts/DisplayUnlockCaptcha and I can log in to the account through a web browser without any problems.
这发生在我的本地开发机器和公众可访问的站点上.这是我的GMail帐户或我的实施问题吗?
This happens on my local development machine and the publically-accesible site. Is this an issue with my GMail account or my implementation?
您需要允许从不太安全的应用访问,因为通过密码进行的IMAP本质上是不安全的. (不可撤消)
You need to allow access from less-secure apps, because IMAP via password is fundamentally insecure. (it's non-revocable)
更好的是,切换到OAuth身份验证.
Better yet, switch to OAuth authentication.