1

I'm having some trouble with swiftmailer. It keeps crashing on me, but if I take out the ->sentTo it doesn't error out but I also can't receive an Email.

I'm confused as to what I am doing wrong.

require_once 'lib/swift_required.php';

// Using smtp
//$transport = Swift_SmtpTransport::newInstance('my_smtp_host.com', 25)

//Using Gmail
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl") 
  ->setUsername('***') // or your gmail username
  ->setPassword('***'); // or your gmail password

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance($subject)
  ->setFrom(array($email => $name))
  ->setTo(array('John.Doe@gmail.com' => 'John Doe'))
   ->setBody($content, 'text/html');

$message->attach(
Swift_Attachment::fromPath($_FILES['userfile']['name'])->setFilename($pic['tmp_name'])
);

// Send the message
$result = $mailer->send($message);

?>

Fatal error:

Uncaught exception 'Swift_TransportException' with message 'Failed to authenticate on SMTP server with username "...." using 2 possible authenticators' in ../lib/classes/Swift/Transport/Esmtp/AuthHandler.php:184 

Stack trace: 

0 ../lib/classes/Swift/Transport/Esmtp/EsmtpTransport.php(312): Swift_Transport_Esmtp_AuthHandler->afterEhlo(Object(Swift_SmtpTransport)) 

1 ../lib/classes/Swift/Transport/AbstractSmtpTransport.php(120): Swift_Transport_EsmtpTransport->_doHeloCommand() 

2 ../lib/classes/Swift/Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() 

3 upload.php(73): Swift_Mailer->send(Object(Swift_Message)) 

4 {main} thrown in lib/classes/Swift/Transport/Esmtp/AuthHandler.php on line 184
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Giovatto
  • 77
  • 9
  • What does "error out" mean? – Álvaro González Feb 19 '13 at 17:21
  • Sorry for my slang. The PHP has a Fatal Error and does not successfully perform the task it is supposed to because of it. – Giovatto Feb 19 '13 at 17:32
  • I think that my problem may lie with the attachment. Maybe I have to pull it after its been moved to the server – Giovatto Feb 19 '13 at 17:36
  • Weird... Fatal errors normally include a description and a line number. Your PHP must be really broken... – Álvaro González Feb 19 '13 at 17:38
  • I sense your sarcasm and I must politely tell you that I don't appreciate it. You could just ask for me to post the error and line number. I assumed that Being such a short code maybe someone could see something that I'm missing. – Giovatto Feb 19 '13 at 17:40
  • 3
    @Giovatto - it's usually best to provide error messages as they appear on your screen, and in the original question too, since people will (nearly) always ask for it. "Old hands" here will have asked people for pertinent information thousands of times... a good approach is to ask oneself: 'if I were answering this question, what information would I ask for?' `:-)` – halfer Feb 19 '13 at 18:05
  • 1
    An excellent answer from @Alvaro has been downvoted - not the OP I hope! - so I would ask readers to upvote it if they like it. – halfer Feb 20 '13 at 08:36
  • 1
    @halfer - Great comment. And it often happens that when the user is extremely reluctant to provide the error message, the message provides a very clear hint on how to fix the error. Whatever, it wasn't my intention to be rude, seriously. – Álvaro González Feb 20 '13 at 08:37

2 Answers2

0

Had to change the message attachment to this:

$message->attach(
    Swift_Attachment::fromPath($theDirectory)->setFilename($fileNameForEmail)
    );

    // Send the message
    $result = $mailer->send($message);
}
else{
        echo 'The file you have chosen is invalid. Please go back and try again.';
}
Giovatto
  • 77
  • 9
0

This:

Failed to authenticate on SMTP server with username "...." using 2 possible authenticators

... means exactly what it says. Gmail did not accept your username and password, either because they are wrong or because you failed to provide some required parameter.

Host name, port and encryption are correct according to other questions at Stack Overflow and my own tests. So probably issues include:

  1. You provided an incorrect user name.
  2. You provided an incorrect password.
  3. If you've enabled two-way authentication, you need to generate an application specific password. This video explains how.

Once you fix this, you'll probably get the following exception:

Uncaught exception 'Swift_IoException' with message 'Unable to open file for reading []'

... triggered by this code:

Swift_Attachment::fromPath($_FILES['userfile']['name'])

The reason if that you are reading the wrong item from the $_FILES array: fromPath() expects a file system path to read but $_FILES['userfile']['name'] contains the original name of the file on the client machine.

Community
  • 1
  • 1
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 1
    My problem was the answer I posted below. Had nothing to do with Google. It said I have to wait 2 days before I can mark my answer correct though. – Giovatto Feb 19 '13 at 18:05
  • 1
    Actually I got a Warning email from google from my other account when I tried to switch to that one because of Hijacking by an outside app. – Giovatto Feb 19 '13 at 18:06
  • 1
    @Giovatto - I've actually installed Swift Mailer in my computer and ran your code against my Gmail account. This *is* the explanation for the error details you've posted. Your code has one more error, though, and I've just edited my answer to explain the second one—but you won't get the second error message until you fix the first one. If you're annoyed with me and want to pretend that error messages are irrelevant, go ahead and accept your answer, but that won't help to make Stack Overflow a reference Q&A site. – Álvaro González Feb 20 '13 at 08:29
  • If you're running it locally no wonder you have an issue. I'm telling you it works perfect i'm not sure what you're doing wrong. – Giovatto Feb 22 '13 at 22:35
  • 1
    @Giovatto - I think it would be better to be kind towards someone who has clearly been helpful to you. Alvaro's 35K reputation on SO suggests a high level of expertise, and that the above analysis is probably correct `:)` – halfer Feb 27 '13 at 10:13