PHP Fatal error: 'Swift_TransportException'

2019-01-23 09:57发布

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:

PHP Fatal error:  Uncaught exception 'Swift_TransportException' with message 'Failed to authenticate on SMTP server with username "xxx@gmail.com" 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.com on port 465 and it worked fine, so it must not be a firewall issue.

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.

Any ideas would be welcome here.

My entire php code:

<?php
require_once 'SwiftMail/lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  ->setUsername('xxx@gmail.com')
  ->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!

3条回答
太酷不给撩
2楼-- · 2019-01-23 10:08

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.

http://www.google.com/accounts/DisplayUnlockCaptcha

查看更多
别忘想泡老子
3楼-- · 2019-01-23 10:08

Welcome to SO. This is more a lengthy comment than an answer. It's great to see you've posted your error message:

PHP Fatal error: Uncaught exception 'Swift_TransportException' with message 'Failed to authenticate on SMTP server with username "xxx@gmail.comm" using 2 possible authenticators' 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 "xxx@gmail.comm" using 2 possible authenticators

I'd say that is a typo you made:

xxx@gmail.comm
             ^

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.

查看更多
干净又极端
4楼-- · 2019-01-23 10:25

You can also check that the 2 steps validation authentication is not enabled. This was the case for me.

查看更多
登录 后发表回答