Hi,

I am trying to use Swift Mailer to send emails from PHP using Gmail's SMTP server.

I've checked some examples, but when I try to send emails I get this message:

Connection to the given MTA failed. The Connection Interface said: Unable to find the socket transport "tls" - did you forget to enable it when you configured PHP?

Not sure what the error is trying to imply by asking if I've configured PHP to use TLS (I think it is a generic error message gone pearshaped). Anyone has used this program and can help please?

Here is the code:

require('Swift.php');
require('Swift/Swift_SMTP_Connection.php');

//The mailer will now establish a connection with the server
$mailer = new Swift(new Swift_SMTP_Connection('smtp.gmail.com', 587, SWIFT_TLS));

//If anything goes wrong you can see what happened in the logs
if ($mailer->isConnected()) //Optional
{
	//You can call authenticate() anywhere before calling send()
	if ($mailer->authenticate('javierh@gmail.com', 'javierh'))
	{
		//Sends a simple email
		$mailer->send(
			'"Joe Bloggs" <javierh@xyz.com>',
			'"Your name" <you@yourdomain.com>',
			'Some Subject',
			"Hello Joe it's only me!"
		);
	}
	else echo "Didn't authenticate to server";

//Closes cleanly... works without this but it's not as polite.
$mailer->close();
}
else echo "The mailer failed to connect. Errors: <pre>".print_r($mailer->errors, 1)."</pre><br />
	Log: <pre>".print_r($mailer->transactions, 1)."</pre>";

Many thanks

    You need to enable the openssl extension because that extension provides the tls socket transport layer.

    Thomas

      Thanks again Thomas. I enabled and stopped and started Apache. Now the error I get is just:

      Connection to the given MTA failed.

      hmmm.... Any ideas on this one? Thanks

        What happens if you remove if ($mailer->isConnected()) ?

        require('Swift.php');
        require('Swift/Swift_SMTP_Connection.php');
        
        //The mailer will now establish a connection with the server
        $mailer = new Swift(new Swift_SMTP_Connection('smtp.gmail.com', 587, SWIFT_TLS));
        $mailer->authenticate('javierh@gmail.com', 'javierh');
        
        if (!$mailer->hasFailed())
        {
            //Sends a simple email
            $mailer->send(
                    '"Joe Bloggs" <javierh@xyz.com>',
                    '"Your name" <you@yourdomain.com>',
                    'Some Subject',
                    "Hello Joe it's only me!"
                    );
        }
        else 
            echo "The mailer failed to send the mail. Errors: <pre>"
                .print_r($mailer->errors, 1)
                ."</pre><br />Log: <pre>"
                .print_r($mailer->transactions, 1)
                ."</pre>"; 
        
        if ($mailer->isConnected())
        {
            $mailer->close();
        }
        

          or replace isConnected() with hasFailed()

            Thanks Thomas, but both fail. Removing IsConnected() simply reports:

            Didn't authenticate to server

            while changing isConnected() to hasFailed() throws up the same (Connection to the given MTA failed.) error...

              What happens if you change the port to 465 ?

                I just tried it with port 465 instead of 587. I don't have a Gmail account but it looked like that the connection gets established on port 465 but not on port 587. I couldn't test the authentication but the error message showed that the connection was ok and authentication failed.

                Thomas

                  You are a genius man. Would you mind explaining to me what happened there?

                  On Thunderbird I have SMTP configured for GMAIL on port 587 and it works a treat. Why is it different from PHP and how did you know?

                    I don't know the exact reason for that behaviour. I browsed the Gmail support site and saw that those two ports are available. I then checked some forums and saw people reporting similar problems on port 587. They didn't have any problems on port 465. I think the communication that Gmail expects on port 587 differs slightly from that on port 465. I have no idea, why...

                    Thomas

                      11 days later

                      Hi sorry to chime into this message so late. I just spotted it in the google index.

                      I'm the author of Swift Mailer 😉

                      Gmail uses something called STARTTLS, which means you can connect on the normal ports like 25 and then in initiate TLS from there. The port to connect to if you just want to jump right in with a TLS connection is 465 -- this is what Swift does presently. There is actually a constant defined for this "SWIFT_SECURE_PORT".

                      Glad you got it sorted anyway 🙂

                        Thanks d11wtg and congrats on Swift Mailer, it works very well!

                          Write a Reply...