我尝试使用Swiftmailer从Gmail发送电子邮件,并打印出此错误:
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Failed to authenticate on SMTP server with username "username@gmail.com" using 2 possible authenticators' in /home/websitename/public_html/cv/Swift-5.1.0/lib/classes/Swift/Transport/Esmtp/AuthHandler.php:184 Stack trace: #0 /home/websitename/public_html/cv/Swift-5.1.0/lib/classes/Swift/Transport/EsmtpTransport.php(312): Swift_Transport_Esmtp_AuthHandler->afterEhlo(Object(Swift_SmtpTransport)) #1 /home/websitename/public_html/cv/Swift-5.1.0/lib/classes/Swift/Transport/AbstractSmtpTransport.php(120): Swift_Transport_EsmtpTransport->_doHeloCommand() #2 /home/websitename/public_html/cv/Swift-5.1.0/lib/classes/Swift/Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() #3 /home/websitename/public_html/cv/profile2.php(69): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in /home/websitename/public_html/cv/Swift-5.1.0/lib/classes/Swift/Transport/Esmtp/AuthHandler.php on line 184
我的PHP代码:
require_once 'Swift-5.1.0/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername('username@gmail.com')
->setPassword('password');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('username@gmail.com' => 'Ahmad Shadeed'))
->setTo(array('receiver@gmail.com' => 'YOU'))
->setBody('This is the text of the mail send by Swift using SMTP transport.');
$numSent = $mailer->send($message);
Anyhelp要解决这个问题吗?
谢谢,
答案 0 :(得分:6)
转到
https://www.google.com/settings/security/lesssecureapps
登录SMTP配置gmail帐户
这里
->setUsername('username@gmail.com')
->setPassword('password');
启用"访问不太安全的应用"