4

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!

Mitreto
  • 41
  • 1
  • 1
  • 3

3 Answers3

24

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

John McCann
  • 599
  • 9
  • 18
  • Welcome to Stack Overflow! Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Dave Chen Jul 03 '13 at 21:48
  • @John, you're solution worked for me. I've been looking for that link and wasn't able to find it. – Jason Lin Jul 11 '13 at 21:44
  • @John Amazing! it resolved my unresolvable problem! just clicking that link. What it is exaclty? cleaning the ip address? – FrancescoMussi Sep 03 '13 at 12:07
  • Do I open the link from my server then? Like how? Is there a command? – Rohan Apr 15 '15 at 18:23
  • 1
    @Rohan, it has been a long time; so, I no longer recall. However, I do know that my code was running on a headless AWS linux sever, so I don't believe I would have hit it from the server. I believe it was as simple as visiting the link from a browser, on any machine, while logged into your google account. – John McCann Apr 16 '15 at 18:38
2

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

Dodger Web
  • 114
  • 1
  • 5
1

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • Thanks for the reply hakre, but this was not the root cause unfortunately. I've tried like millions of different things and just copy/pasted the wrong error message. My credentials are correct and the error still the same :/ – Mitreto Oct 02 '12 at 07:11
  • Hmm, sounds like debugging work. try to get an overview and plan how to approach your issue point by point to trouble shoot it. Swiftmailer should offer you some logs for debugging, use them. This is probably a network issue. – hakre Oct 02 '12 at 07:16
  • What network issue could it be besides a firewall setting? Any idea? – Mitreto Oct 02 '12 at 07:59
  • Okay, let's troubleshoot this step by step: It looks like that Swiftmailer tries to authenticate but can not. So a network connection is likely to be established already. Which kind of authentication is google mail offering and which are the two types of authentication you're using via Swiftmailer? Also: You said you've verified your credentials, how did you do that. – hakre Oct 02 '12 at 09:18
  • You wrote you've read all the posts out there. Such kind of note is merely saying nothing next to that you're trying. Have you looked into this post? http://stackoverflow.com/questions/3478906/using-phps-swiftmailer-with-gmail ? How are you setting your username and password? Your code only shows which Servername and Port you use. There seems to be a difference how you can write the username, please see the comments on: http://stackoverflow.com/questions/12665915/php-pear-mail-fails-using-google-apps-mail-account-error-535-authentication-fa?lq=1 – hakre Oct 02 '12 at 09:22
  • thaqt's better, yes. so now for logging. could you find out more in the manual about enabling logging? Please add that code, too and the log related to sending the email. See http://swiftmailer.org/docs/plugins.html and look for the Logger plugin. Especially the note about the SMTP errors. That should give you some better feedback. – hakre Oct 04 '12 at 08:12