PHP 致命错误:“Swift_TransportException”,消息“无法在 SMTP 服务器上进行身份验证”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12663783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Fatal error: 'Swift_TransportException' with message 'Failed to authenticate on SMTP server
提问by Mitreto
I know this question's been asked before and I've read all the posts out there, but I still can't find a solution to this.
我知道以前有人问过这个问题,我已经阅读了所有帖子,但我仍然找不到解决方案。
I have a windows machine with wamp installed on it. When I try to send a simple email via google's SMTP server everything works fine. Though, when I copy that same script to an Ubuntu 12 machine, it gives me that error:
我有一台装有 wamp 的 Windows 机器。当我尝试通过 google 的 SMTP 服务器发送简单的电子邮件时,一切正常。但是,当我将相同的脚本复制到 Ubuntu 12 机器时,它给了我这个错误:
PHP Fatal error: Uncaught exception 'Swift_TransportException' with message 'Failed to authenticate on SMTP server with username "[email protected]" using 2 possible authenticators' in /home/TestMail/SwiftMail/lib/classes/Swift/Transport/Esmtp/AuthHandler.php:171
Stack trace:
/home/TestMail/SwiftMail/lib/classes/Swift/Transport/EsmtpTransport.php(289): Swift_Transport_Esmtp_AuthHandler->afterEhlo(Object(Swift_SmtpTransport))
/home/TestMail/SwiftMail/lib/classes/Swift/Transport/AbstractSmtpTransport.php(114): Swift_Transport_EsmtpTransport->_doHeloCommand()
/home/TestMail/SwiftMail/lib/classes/Swift/Mailer.php(76): Swift_Transport_AbstractSmtpTransport->start()
/home/TestMail/testmail.php(73): Swift_Mailer->send(Object(Swift_Message))
thrown in /home/TestMail/SwiftMail/lib/classes/Swift/Transport/Esmtp/AuthHandler.php on line 171
That's how I initialize the transport:
这就是我初始化传输的方式:
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
I've tried to telnet to smtp.gmail.comon port 465 and it worked fine, so it must not be a firewall issue.
我尝试通过 telnet 连接到smtp.gmail.com端口 465 并且它工作正常,所以它一定不是防火墙问题。
I have SSL enabled with PHP. I tried to send two separate mails with and without SSL with a different mail server and everything worked like charm. It is only google's mail that gets me mad.
我用 PHP 启用了 SSL。我尝试使用不同的邮件服务器发送两封带有和不带有 SSL 的单独邮件,一切都像魅力一样。只有谷歌的邮件让我生气。
Any ideas would be welcome here.
这里欢迎任何想法。
My entire php code:
我的整个 php 代码:
<?php
require_once 'SwiftMail/lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername('[email protected]')
->setPassword('xxx');
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
$htmlbody = 'some html here';
// Create a message
$message = Swift_Message::newInstance('without head')
->setFrom(array('<from email>' => '<some sender>'))
->setTo(array('<to email>' => '<some recepient>'))
->setBody($htmlbody, 'text/html');
// Send the message
$result = $mailer->send($message);
var_dump($result);
?>
Thanks!
谢谢!
回答by John McCann
This is an old thread, but my resolution was a bit different for the same error. Turns out my Swift configuration is fine. The IP from my server was blocked by Google as suspicious. I was able to clear it by visiting this link, then executing my mailer code from the server.
这是一个旧线程,但对于相同的错误,我的解决方案略有不同。事实证明我的 Swift 配置很好。来自我服务器的 IP 被谷歌以可疑身份阻止。我可以通过访问此链接来清除它,然后从服务器执行我的邮件程序代码。
回答by Dodger Web
You can also check that the 2 steps validation authentication is not enabled. This was the case for me.
您还可以检查是否未启用 2 步验证身份验证。这就是我的情况。
回答by hakre
Welcome to SO. This is more a lengthy comment than an answer. It's great to see you've posted your error message:
欢迎来到 SO。这与其说是一个答案,不如说是一个冗长的评论。很高兴看到您发布了错误消息:
PHP Fatal error: Uncaught exception 'Swift_TransportException' with message 'Failed to authenticate on SMTP server with username "[email protected]" using 2 possible authenticators' in /home/TestMail/SwiftMail/lib/classes/Swift/Transport/Esmtp/AuthHandler.php:171
PHP 致命错误:未捕获的异常 'Swift_TransportException' 带有消息 'Failed to authentication on SMTP server with username "[email protected]" using 2 possible authenticationators' in /home/TestMail/SwiftMail/lib/classes/Swift/Transport/Esmtp/ AuthHandler.php:171
Part of it is the actual message :
其中一部分是实际消息:
Failed to authenticate on SMTP server with username "[email protected]" using 2 possible authenticators
无法使用 2 个可能的身份验证器在用户名“[email protected]”的 SMTP 服务器上进行身份验证
I'd say that is a typo you made:
我会说这是你犯的一个错字:
[email protected]
^
Can happen. Please verify you're using the right login credentials. Also even if you find similar looking questions on site, it must not mean they cover your problem.
可以发生。请确认您使用的是正确的登录凭据。此外,即使您在网站上发现类似的问题,也不意味着它们涵盖了您的问题。
Hopefully this helps, otherwise try to provide more error information, e.g. is there a log with swiftmailer? Have you double checked the credentials and the server configuration? What's the firewall doing? And so on and so forth.
希望这会有所帮助,否则尝试提供更多错误信息,例如是否有 swiftmailer 的日志?您是否仔细检查了凭据和服务器配置?防火墙在做什么?等等等等。

