Sending email using php, gmail, and swiftmailer causes SSL-related error
Asked Answered
C

7

9

Here is my PHP code:

function SendCookieToTheMail()
{
    require_once 'swift-mailer/lib/swift_required.php';
    //Create the Transport
    $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com')
      ->setPort(465)
      ->setEncryption('ssl')
      ->setUsername('[email protected]')
      ->setPassword('123')
      ;

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

    //Create a message
    $message = Swift_Message::newInstance('Test')
      ->setFrom(array('[email protected]' => 'From mr. 007'))
      ->setTo(array('[email protected]', '[email protected]' => 'To mr. 007'))
      ->setBody('Body')
      ;

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

    /*
    You can alternatively use batchSend() to send the message

    $result = $mailer->batchSend($message);
    */ 
}

Here is the error:

( ! ) Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?) in C:\Program Files\wamp\www\swift-mailer\lib\classes\Swift\Transport\StreamBuffer.php on line 233

( ! ) Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? #44551400]' in C:\Program Files\wamp\www\swift-mailer\lib\classes\Swift\Transport\StreamBuffer.php on line 235

( ! ) Swift_TransportException: Connection could not be established with host smtp.gmail.com [Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? #44551400] in C:\Program Files\wamp\www\swift-mailer\lib\classes\Swift\Transport\StreamBuffer.php on line 235

Where is the problem??

Update:

I checked phpinfo() and it says:

OpenSSL support     disabled (install ext/openssl) 

I referred to the links below, but I couldn't install ssl...

Curson answered 31/1, 2011 at 0:24 Comment(0)
O
3

Did your php support SSL ? http://php.net/manual/en/function.fsockopen.php, and check http://www.php.net/manual/en/openssl.installation.php for reference.

Create a page with

phpinfo();

Is SSL enabled?

Oldie answered 31/1, 2011 at 0:29 Comment(0)
S
16

i Was searching for a similar question and i found that you have to edit the php.ini file edit the following line

;extension=php_openssl.dll

remove the semi colon and it will work fine

Hope that help any one else :)

Statis answered 17/12, 2011 at 9:0 Comment(0)
O
3

Did your php support SSL ? http://php.net/manual/en/function.fsockopen.php, and check http://www.php.net/manual/en/openssl.installation.php for reference.

Create a page with

phpinfo();

Is SSL enabled?

Oldie answered 31/1, 2011 at 0:29 Comment(0)
C
2

gmail need this in your config.yml

swiftmailer: encryption: tls

or replace your: ->setEncryption('ssl') by ->setEncryption('tls')

and not ssl

Cochleate answered 6/1, 2013 at 16:45 Comment(0)
S
1

You should enable php_openssl module from php extensions. Just edit your php.ini file

extension=php_openssl.dll
Squash answered 5/1, 2013 at 17:57 Comment(0)
R
1

in fact, I advice to use tls on port 25 test using the following syntax:

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 25, 'tls')
    ->setUsername('[email protected]')
    ->setPassword('123');
Riotous answered 22/10, 2013 at 13:54 Comment(0)
S
0

You need to configure php to work with ssl

http://www.php.net/manual/en/openssl.installation.php

Smegma answered 31/1, 2011 at 0:43 Comment(0)
M
0

I hope that you have solved your issue, however for me line:

;extension=php_openssl.dll

did not exist in my php.ini (running XAMPP 1.7.7 on Win7), so just go ahead and add it in the extensions section, remove semicolon from it and it should work.

Misgovern answered 2/6, 2012 at 19:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.